This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to convert int into a char?

 
Jean Paul Martin
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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?
eg:
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the char '3' and not the byte value of 3 then do this:

[ April 06, 2004: Message edited by: Michael Morris ]
 
Jean Paul Martin
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your prompt reply. I appreciate. Have a good one.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael got you on the right track...
now how would you handle numbers greater than 9?? two-digit numbers? three-digit numbers?
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about negative numbers?
 
Madhav Bhargava
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about first converting int to Integer
From Integer get a String
From String -- method toCharArray() gets you a character array...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic