neelabh
neelabh

Reputation: 577

Android NumberFormatException error with a simple number input from EditText

So I have a simple Edit-text with number input. User enters his phone number and it is saved into DB obviously that is the goal. now every time i do

int contactNumber=Integer.parseInt(mContactNumber.getText().toString());

I get an error thrown saying NumberFormatException for some reason it doesn't like a string of number. I have made sure that mContactNumber field in the android input field has the

android:input="number"

now i also tried just to be sure

int contactNumber=Integer.parseInt(mContactNumber.getText().toString().trim());

still the same error

Guys any ideas?

Upvotes: 1

Views: 2869

Answers (4)

Junaid
Junaid

Reputation: 1179

Try putting the Type to ' long ' instead of ' int '. Hope that will work for you.

Upvotes: 5

Dinesh Prajapati
Dinesh Prajapati

Reputation: 9510

For phone Number you can not take it as Integer. try to take it as string only and after getting that string you can do the check for the numbers only by

TextUtils.isDigitsOnly(str);

Upvotes: 2

Paresh Mayani
Paresh Mayani

Reputation: 128428

You need to put a simple condition:

if(mContactNumber.getText().length()>0)
{
    int contactNumber=Integer.parseInt(mContactNumber.getText().toString().trim());
}

Upvotes: 2

Shadow
Shadow

Reputation: 6277

This might be because you are leaving the text field empty. Are you sure you are not parsing an empty string?

Upvotes: 3

Related Questions