0

I am trying to get integer value entered in EditText by user.

EditText eTextValue=(EditText) findViewById(R.id.eId); 
String eTextString=eTextValue.getText().toString();
int eTextValue1=Integer.parseInt(eTextString);

But unluckily I am getting,

unable to parse 9827328 as integer.

I have tried using Integer.valueOf instead of Integer.parseInt but again I am getting the same exception.

I have even used Long datatype to store value instead of int type datatype but nothing seems to be working.Any help over this will be highly appreciated. I have gone through all these links unable to parse ' ' as integer in android , Parsing value from EditText...but nothing seems to be working all of them are landing me in exception.

Community
  • 1
  • 1
Saurabh
  • 975
  • 2
  • 12
  • 27
  • Can you post your Exception Stacktrace? – Srinivas Dec 25 '12 at 15:04
  • 3
    The number you enter may have leading and/or trailing white-spaces. Try using the `trim()` method. – Lion Dec 25 '12 at 15:09
  • 3
    Try with android:inputType="number" in xml.Also see.. http://stackoverflow.com/questions/4903515/how-do-i-return-an-int-from-edittext-android http://stackoverflow.com/questions/2709253/converting-a-string-to-an-integer-android – ridoy Dec 25 '12 at 15:13
  • Thanks Ridoy your solution works fine for me.I very much impressed by the intelligence of Android when it does not let the user enter any other value than Integer in EditText to avoid exception. – Saurabh Dec 25 '12 at 15:41

4 Answers4

4

You are using eTextValue as a variable name for two different things (an int and an EditText). You cant do that and expect it to work properly. Change one or the other and it should work better.

Barak
  • 16,318
  • 9
  • 52
  • 84
0
int id;
id=Integer.parseInt(ed.getText().toString());
Pang
  • 9,564
  • 146
  • 81
  • 122
yuva ツ
  • 3,707
  • 9
  • 50
  • 78
  • have you change variable names of edittext and int varible? its working at my place – yuva ツ Dec 25 '12 at 15:53
  • Yes I tried but the only solution works in my place is http://stackoverflow.com/questions/2709253/converting-a-string-to-an-integer-android.. – Saurabh Dec 25 '12 at 16:18
0

Try by entering the following in the XML File under the corresponding EditText element.

android:inputType="number"

Hope this should get you pass the exception.

Gridtestmail
  • 1,459
  • 9
  • 10
0

You need to check whether the string you are parsing is an integer. Try this code:

if (IsInteger(eTextString)
   int eTextValue1=Integer.parseInt(eTextString);

and add this function:

public static boolean IsInteger(String s)
{
   if (s == null || s.length() == 0) return false;
   for(int i = 0; i < s.length(); i++)
   {
      if (Character.digit(s.charAt(i), 10) < 0)
         return false;
   }
   return true;
}

I hope this helps

Dan Bray
  • 7,242
  • 3
  • 52
  • 70