• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion from String to int

 
Richard Robbins
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Thomas Berglund
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or...

in a single step.
(watch out for the NumberFormatException )
Dave.
 
Richard Robbins
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic