Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2006

Tip: Looking for answers? Try searching our database.

Convert from int to char

Thread view: 
Mocha_Java - 25 Sep 2006 21:20 GMT
Okay, so i have a dilemma. I have a int variable called rank that
stores integers (obviously). however, i need to display these values as
chars in another method. casting dosent help because it just converts
the integer  into the unicode character which isn't the same as the
number.

heres a snippet of the problem:

char r;                      // as you can see, its gets pretty
repetitive and boring
if (rank == 2)
    r = '2';
if (rank == 3)
   r = '3';
if (rank == 4)
  r = '4';

....
return r;

is there a trick to make my int number the equivalent char number?
Patricia Shanahan - 25 Sep 2006 23:32 GMT
> Okay, so i have a dilemma. I have a int variable called rank that
> stores integers (obviously). however, i need to display these values as
[quoted text clipped - 17 lines]
>
> is there a trick to make my int number the equivalent char number?

Does Character.forDigit() do what you want?

Patricia
IchBin - 26 Sep 2006 00:37 GMT
> Okay, so i have a dilemma. I have a int variable called rank that
> stores integers (obviously). however, i need to display these values as
[quoted text clipped - 17 lines]
>
> is there a trick to make my int number the equivalent char number?

Roedy has an online applet, on his website, that will let you select the
from and to Object types and offer the available Java statement to do
the conversion.

http://mindprod.com/applets/converter.html

You can also download it also.

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA              http://weconsultants.phpnet.us
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

Jeff - 26 Sep 2006 02:55 GMT
> Okay, so i have a dilemma. I have a int variable called rank that
> stores integers (obviously). however, i need to display these values as
[quoted text clipped - 17 lines]
>
> is there a trick to make my int number the equivalent char number?

I'm going to guess that you really mean that you want to convert it to
a character string, to display in a textbox or the like.

The trick is to use String.format

To convert your integer to a string, use
String r = String.format("%d", rank);

hth
/js
Tor Iver Wilhelmsen - 27 Sep 2006 17:44 GMT
> is there a trick to make my int number the equivalent char number?

char r = '0' + rank;
Joan C - 30 Sep 2006 15:25 GMT
> Okay, so i have a dilemma. I have a int variable called rank that
> stores integers (obviously). however, i need to display these values as
[quoted text clipped - 17 lines]
>
> is there a trick to make my int number the equivalent char number?

The following single line should do what you want:

char r = (char) (rank+48);

as the ascii characters 0-9 equte to 48, 49, 50 ... 57.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.