• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting int to String

 
Ian Cockcroft
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I am accessing a database and trying to populate a JComboBox with int's, but I get an error telling me to convert the int to a String.
When I cast it to a String like this:
String OrderNo = (String)(rs.getInt("OrderNo"));
I get an error.
What am I doing wrong and how can I fix it?
Thanks alot,
Ian
my code looks like this,
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ian,
According to The Java Language Specification, �5.5 - Casting Conversion: "a cast can do any permitted conversion other than a string conversion".
Take a look at String::valueOf(int) or Integer::toString(int).
Good Luck.
 
Ian Cockcroft
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dirk, thanks for the help, but I am getting a compiler error telling me it doesnt know aboout toString().
Do I have to import something else?
I am importing the following:
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.sql.*;
import java.awt.event.*;

and the code:

Any ideas?
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had you actually followed the link provided you would have saw that toString(int) is a static method of the Integer class and coded like so:

ps Just giving you a hard time
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
toString(int) is a static method of the class Integer (which is part of the package java.lang and as such does not require an import statement). You can't invoke a method from a class that is not the current class or a parent of the current class without specifying the class or object to which the method belongs. So, to use the static method toString(int) from the class Integer, you could do so like this:
String string = Integer.toString(number);
Note that import statements can be used to import classes directly or packages of classes, but not the specific methods of a class.
Is this making sense?
[ April 23, 2002: Message edited by: Dirk Schreckmann ]
 
Ian Cockcroft
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Dirk,
It works perfectly, I will have to have a look at what you said. I thought java.lang.* was imported automatically?
Thanks for all you help.
Regards
Ian
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dirk Schreckmann:
toString(int) is a static method of the class Integer (which is part of the package java.lang and as such does not require an import statement).

Isn't that what he said, Ian?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic