• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Char to String

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic