Forums Register Login

Converting Chars to Strings

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
if Java doesnt support Operator Oeverloading than how can v use + operator for addition and String concatenation???
+Pie Number of slices to send: Send
 

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 ]
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 895 times.
Similar Threads
Anagrams
Why can student[x] be resolved?
not a statement
Bouncycastle AES - output isn't the same as input!
to Dave Lander,Marilyn de Queiroz
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:38:04.