Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Eung maeng
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Does anyone explain to me how to convert from string("Y") to char?

THANKS
 
John Lockheart
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic