| Author |
Converting Chars to Strings
|
Aaron Parker
Greenhorn
Joined: Jan 31, 2004
Posts: 27
|
|
Ok, this is really frustrating. According to the api, I can use the toString method to convert chars to strings so I can use the equals method to compare the strings. Problem code: (inside a loop) if (tempGrade.equals(validGrades[z])) { found = true; tempGrade and validGrades[] both contain chars, and I want to compare them. I get this compile error when it's run like this: src/java192/project3/GradePoint.java:46: char cannot be dereferenced if (tempGrade.equals(validGrades[z])) { ^ If I try to use the toString method I get these errors with this code: String tempGradeString = toString(tempGrade); String validGradesString = toString(validGrades[z]); if (tempGradeString.equals(validGradesString)) { found = true; src/java192/project3/GradePoint.java:44: toString() in java.lang.Object cannot b e applied to (char) String tempGradeString = toString(tempGrade); ^ src/java192/project3/GradePoint.java:45: toString() in java.lang.Object cannot b e applied to (char) String validGradesString = toString(validGrades[z]); ^ 2 errors Why can't Java just have a standard set of conversions to go between types? Short of an answer to that, how can I get this comparison to work? Aaron Parker
|
 |
Aaron Parker
Greenhorn
Joined: Jan 31, 2004
Posts: 27
|
|
Never mind . Figured it out. Not why Java can't have conversions that make sense, but the part about getting it to work! Thanks, anyway.
|
 |
Michael Morris
Ranch Hand
Joined: Jan 30, 2002
Posts: 3451
|
|
Why can't Java just have a standard set of conversions to go between types? Because Java is an OO language and operator-overloading is not allowed. All meaningful conversions can be done anyway but you don't do it procedurally like in C or C++ which does allow you to overload the = operator. You don't have any global methods in Java (unless you consider static methods to be global) so you must have an instance of a class, you can't just write String tempGradeString = toString(tempGrade);. You can do this: String tempGradeString = new String(tempGrade); which accomplishes what you wish.
|
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
|
 |
Kamlesh Sangani
Greenhorn
Joined: Feb 28, 2004
Posts: 22
|
|
if Java doesnt support Operator Oeverloading than how can v use + operator for addition and String concatenation???
|
Lets Java !<br />Kamlesh
|
 |
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
|
|
Originally posted by KS: if Java doesnt support Operator Oeverloading than how can v use + operator for addition and String concatenation???
The '+' operator is overloaded implicitly by the language to handle string concatenation. You can't explicitly overload/override it. [ March 08, 2004: Message edited by: Vicken Karaoghlanian ]
|
- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
|
 |
 |
|
|
subject: Converting Chars to Strings
|
|
|