This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to convert int into a char? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to convert int into a char?" Watch "How to convert int into a char?" New topic

How to convert int into a char?

Jean Paul Martin
Ranch Hand

Joined: Oct 05, 2003
Posts: 30
I have a problem that sounds simple, but I have been struggling how to figure it out. How to convert an integer in java into a char, but keeping the value of the integer?
int x = 3;
so i want to make a char y = '3' by converting x.
if i do, char y = (char)x; the value of y is no longer 3!! Please help.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
If you want the char '3' and not the byte value of 3 then do this:

[ April 06, 2004: Message edited by: Michael Morris ]

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jean Paul Martin
Ranch Hand

Joined: Oct 05, 2003
Posts: 30
Thank you for your prompt reply. I appreciate. Have a good one.
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

Michael got you on the right track...
now how would you handle numbers greater than 9?? two-digit numbers? three-digit numbers?

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
What about negative numbers?

SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Madhav Bhargava

Joined: Apr 30, 2003
Posts: 6
How about first converting int to Integer
From Integer get a String
From String -- method toCharArray() gets you a character array...
subject: How to convert int into a char?
It's not a secret anymore!