File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Conversion from 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 "Conversion from String to int" Watch "Conversion from String to int" New topic

Conversion from String to int

Richard Robbins
Ranch Hand

Joined: Mar 07, 2001
Posts: 66
Is there a quick and easy way to convert a String (received using the Edit.getText() function) to an int, or does the conversion have to be done manually?

Richard Robbins is one of the founders of, a resource for free website templates and Photoshop tutorials. He also consults for an online boutique selling baptism dresses and christening gowns.
Thomas Berglund

Joined: May 14, 2001
Posts: 1
You can use the Integer class to convert a String (containing digits only) to an Integer object. You can then get the primitive int value from the Integer object, e.g:
String s = "1234";
Integer intObject = new Integer(s);
int i = intObject.intValue();
[This message has been edited by Thomas Berglund (edited May 14, 2001).]
David O'Meara

Joined: Mar 06, 2001
Posts: 13459


in a single step.
(watch out for the NumberFormatException )
Richard Robbins
Ranch Hand

Joined: Mar 07, 2001
Posts: 66
I have been getting the NumberFormatException whenever I run this code:

Integer stringValue = new Integer(Repeat.getText());
Repeat is an edit box.
Richard Robbins
Ranch Hand

Joined: Mar 07, 2001
Posts: 66
I solved the problem. I was using the name of my label (Repeat) rather than the name of the actual edit box (Number), so I was passing the String "Repeat" as a paramater to a new Integer.
I agree. Here's the link:
subject: Conversion from String to int
It's not a secret anymore!