Hi all, I want to change a char "firstnum" to a string. Here is my code. private static String firstNumber( String s ) { char firstnum = s.charAt( 0 ); String x = Character.toString( firstnum ); return x; } And I get the error message: cannot resolve symbol symbol: method toString (char) location: class java.lang.Character String x = Character.toString( firstnum ); which makes it look like the method toString doesn't exist in the Character Class but it is in the method list in the API. I saw that all of the other methods in the API seemed to use the arguments in the method (in the brackets) but the toString method didn't have anything in the brackets in the API. I can use the boolean isDigit and everything works fine but not the toString method seems to be different. Any help is much appreciated! Thanks for your time Don Croswell What am I doing wrong.
ryan burgdorfer
Ranch Hand
Joined: Jan 24, 2001
Posts: 219
posted
0
Perhaps you need to initialize firstnum as a Character (since you're calling a static method of the Character class on firstnum), like so: <h4>Character firstnum = new Character( s.charAt( 0 ) ) ;</h4> Please reply wether or not this works for you... ------------------
If that does not work, try changing this instead: <h4>String x = firstnum.toString() ;</h4> (without implementing my first suggestion) As you may have guessed, both of these suggestions are off the top of my head, without consulting the API - If neither of them works, I'll go to the API at that point Lazily, ------------------
Ryan Burgdorfer
Java Acolyte in
Columbus, OH USA
donald croswell
Greenhorn
Joined: Mar 23, 2001
Posts: 6
posted
0
Hey thanks for such a quick reply! I tried using Character like this: private static String firstNumber( String s ) { Character firstnum = new Character(s.charAt( 0 )); String x = Character.toString( firstnum ); return x; } and got the same error message as the first. I am assuming that I am just using this method the wrong way. As for your second suggestion, I had tried that already. Then I used your first suggestion with the second and....It worked!! It looks like this: private static string firstNumber( String s ) { Character firstnum = new Character(s.charAt( 0 )); String x = firstnum.toString(); return x; } Thank you sooo much for your help!! Don
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6913
posted
0
Could you not have just returned a substring of the string starting at the start of length 0:
This avoids creating a new temporary Character object, and creating new objects to clutter up the heap is generally not a good idea. It also seems simpler to me
Frank, That's why you're a sheriff I guess I was concentrating on figuring out how to solve the problem by correcting the given char-related methods, instead of looking at a more efficient way of solving the problem as a whole... I have to work on being able to step back and look at the big picture. ------------------
Ryan Burgdorfer
Java Acolyte in
Columbus, OH USA
Francisco I
Ranch Hand
Joined: Mar 27, 2001
Posts: 44
posted
0
Hi. I like the option of replacing the whole function to: return s.substring(0,1); Now, the truth is that the method toString(char x) does not exist in the Character class. The method is toString(). No arguments, so what you would need to do if you want to keep using the Character class and the toString method is: String x = new Character(firstnum).toString();
Hope this helps. Francisco. Hi all, I want to change a char "firstnum" to a string. Here is my code. private static String firstNumber( String s ) { char firstnum = s.charAt( 0 ); String x = Character.toString( firstnum ); return x; } And I get the error message: cannot resolve symbol symbol: method toString (char) location: class java.lang.Character String x = Character.toString( firstnum ); which makes it look like the method toString doesn't exist in the Character Class but it is in the method list in the API. I saw that all of the other methods in the API seemed to use the arguments in the method (in the brackets) but the toString method didn't have anything in the brackets in the API. I can use the boolean isDigit and everything works fine but not the toString method seems to be different. Any help is much appreciated! Thanks for your time Don Croswell What am I doing wrong.