• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting Chars to Strings  RSS feed

 
Aaron Parker
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kamlesh Sangani
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if Java doesnt support Operator Oeverloading than how can v use + operator for addition and String concatenation???
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!