• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String to int

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic