It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Char to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Char to String" Watch "Char to String" New topic
Author

Char to String

donald croswell
Greenhorn

Joined: Mar 23, 2001
Posts: 6
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
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...
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
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
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: 6920
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


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
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
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Char to String