aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Conversion from String to int" Watch "Conversion from String to int" New topic
Author

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 WebsiteTemplateDatabase.com, a resource for free website templates and Photoshop tutorials. He also consults for an online boutique selling baptism dresses and christening gowns.
Thomas Berglund
Greenhorn

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
Rancher

Joined: Mar 06, 2001
Posts: 13459

Or...

in a single step.
(watch out for the NumberFormatException )
Dave.
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: http://aspose.com/file-tools
 
subject: Conversion from String to int
 
Similar Threads
Byte Wrapper class
Question for Marcus
Is there any way to convert an int to a float ?
How to determine if argument passed is int?
integer to string conversion