This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to convert from string( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to convert from string("a") to char.." Watch "how to convert from string("a") to char.." New topic
Author

how to convert from string("a") to char..

Eung maeng
Ranch Hand

Joined: Feb 10, 2002
Posts: 68
Hi,
Does anyone explain to me how to convert from string("Y") to char?

THANKS
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
String y = "Y";
char y_char = y.charAt(0);

And if the string is bigger than 1 letter you can do the same thing with a loop or break up the string and send it into a character array with the following:

String y = "hello";
char[] array = y.toCharArray();

array = {'h', 'e', 'l', 'l', 'o'}
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

I plan to use the following method to convert String to char:



This code doesn't throw an exception, but it doesn't print the null characters either.

Is this OK?

Kaydell
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to convert from string("a") to char..