1

I have a problem that made me confused, i want to use the value of EditText and convert it to int, when launching the activity it displays on logcat "invalid int" and i make the input value for the editText is 9 digits! could you help me in finding the problem

this is my code:

     String texti =  numberi.getText().toString();
     int x = Integer.parseInt(texti);
6
  • Integer x = Integer.valueOf(str); are u try this????? Commented Feb 22, 2013 at 5:06
  • can you do a trim on string before doing parseInt. Just to eliminate any chances of space character or so Commented Feb 22, 2013 at 5:07
  • where did u write this code. in onCreate() method ? Commented Feb 22, 2013 at 5:07
  • yes, the problem that i used this way, and for some reason it doesn't work!!! Commented Feb 22, 2013 at 5:09
  • yes i wrote it in OnCreate() Commented Feb 22, 2013 at 5:09

7 Answers 7

1

u said that u have wrote that in onCreate(). so u have to check first, is the text is null or not:

String texti =  numberi.getText().toString();
long x;
if(texti.trim().length() > 0)
  x = Long.parseLong(texti);
Sign up to request clarification or add additional context in comments.

Comments

1

You have to save it in long

long x = Long.parseLong(texti);

Comments

1

If your only objective is to allow user to input numbers into EditText.Then use

android:inputType="number"

And then Integer.parseInt(editText.getText().toString());

Comments

0

The integer in texti could be larger than Integer.MAX_VALUE value, so use Long instead

long x = Long.parseLong(texti.trim());

Comments

0

What you have code is correct but for better way use this,

  String texti =  numberi.getText().toString();
  int x = Integer.parseInt(texti.trim());  // Trim the extra spaces, if any.

Always have habbit to trim() when converting to Numeric format, This will prevent from NumberFormatException

Comments

0

Parsing String to Integer:

    Integer.parseInt(<Your EditText>.getText().toString());

Comments

0

Ideally, if your EditText will only accept integers then you should set inputType property to either:

  • numberSigned
  • number

eg. something like this:

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editTextCustomerPin"
        android:layout_marginTop="20dp"
        android:inputType="numberSigned"
        android:textSize="16dp" android:hint="@string/enterCustomerPin"
        android:layout_gravity="center_horizontal"/>

Now in your activity, you can always do:

EditText myEditText = (EditText) findViewById(R.id.editTextCustomerPin);
String editTextContent = myEditText.getText().toString();
if (editTextContent.length() > 0) {
     int num = Integer.parseInt(num);
     //Do your processing
} else {  
     //Show error message, eg. a toast or an alert dialog
}

Comments

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.