Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String to int

 
Peter Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
v.add(dl);
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
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
Java-help.com
[ October 29, 2002: Message edited by: Jon Huhtala ]
 
Peter Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(...)
Ritu
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic