File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Casting Problem int to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Casting Problem int to String" Watch "Casting Problem int to String" New topic
Author

Casting Problem int to String

Reggie McDougal
Ranch Hand

Joined: Sep 27, 2004
Posts: 69
I can't cast an int to a string so I can compare the values.

I get an incovertable types error when In try the assign the value of i to j as a string so I can check that the value matched in an option select box.

Can someone point me in the right direction.



You can never drink too much
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Reggie,

can't cast an int to a string so I can compare the values.



You can check out the Java API for more info.

Joyce
[ October 25, 2004: Message edited by: Joyce Lee ]
Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
Hi Reggie,
I think you got error when you do this...



You cannot convert int primitive data type to String object directly..
You have to use wrapper class to do that, example



Hope this helps...
Correct me if I am wrong
daniel
Sean Stephens
Ranch Hand

Joined: Oct 25, 2004
Posts: 40
Here's another way to do it, though probably slower than using the Integer wrapper class:

String j = ""+i;
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Sean Stephens:
Here's another way to do it, though probably slower than using the Integer wrapper class:

String j = ""+i;


This is an ugly hack - don't do it.

Another valid way to do it is

String.valueOf(i)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Casting Problem int to String