wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Converting int to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting int to String" Watch "Converting int to String" New topic
Author

Converting int to String

Ian Cockcroft
Greenhorn

Joined: Feb 17, 2002
Posts: 29
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

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ian Cockcroft
Greenhorn

Joined: Feb 17, 2002
Posts: 29
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

Joined: May 17, 2001
Posts: 2823
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

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Feb 17, 2002
Posts: 29
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

Joined: Jul 22, 2000
Posts: 9046
    
  10
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?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
 
subject: Converting int to String