The moose likes Beginning Java and the fly likes String to int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String to int" Watch "String to int" New topic

String to int

Peter Simon

Joined: Oct 25, 2002
Posts: 14
Hi ,
I need to convert an int to a String.
I try this:
Cabin cab = home.findByPrimaryKey(pk);
if((cab.getShip()== shipID) && (cab.getBedCount()== bedCount))
String dl = Integer.toString(cab.getDeckLevel());
Compiler compplains this isn't a statement.
When I replace cab.getDeckLevel()with an int it works fine.
The getDeckLevel method goes like this:
public int deckLevel;
public int getDeckLevel() {
return deckLevel;
would someone have any suggestions?
Jon Huhtala

Joined: Apr 10, 2002
Posts: 17
Peter, give this a try:
String dl =
new Integer(cab.getDeckLevel()).toString();
This wraps the int within an Integer "wrapper" and calls the object's toString() method to return a String.
I have more information on wrapper classes on the Web at:
[ October 29, 2002: Message edited by: Jon Huhtala ]

- Jon Huhtala, SCPJ2<p> "Nothing worthwhile is ever easy..."
Peter Simon

Joined: Oct 25, 2002
Posts: 14
I did that, but the problem does not seem to the conversion, but the method it's using.
when I Write:
int gh = new Integer(cab.getDeckLevel());
compiler complains: '.class' expected.
My method (in it's defenition)returns an int, but for some reason it seems to return an object.
or whatever it returns...
Ritu Kama
Ranch Hand

Joined: Sep 10, 2001
Posts: 72
That's because you are creating an Integer object by new Integer(...) and assigning it to a normal int.
Change it to
Integer int = new Integer(...)
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403

If the above is the exact code you're trying to compile, it could be that the variable "dl" is out of scope.
It gets tricky when you use if/while/for statement blocks without {}
try changing it to this:
I agree. Here's the link:
subject: String to int
It's not a secret anymore!