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.

4
  • Can you post your Exception Stacktrace? Commented Dec 25, 2012 at 15:04
  • 3
    The number you enter may have leading and/or trailing white-spaces. Try using the trim() method. Commented Dec 25, 2012 at 15:09
  • 3
    Try with android:inputType="number" in xml.Also see.. stackoverflow.com/questions/4903515/… stackoverflow.com/questions/2709253/… Commented Dec 25, 2012 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. Commented Dec 25, 2012 at 15:41

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

0
int id;
id=Integer.parseInt(ed.getText().toString());

2 Comments

have you change variable names of edittext and int varible? its working at my place
Yes I tried but the only solution works in my place is stackoverflow.com/questions/2709253/…..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.