Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

You need to put a simple condition:

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

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top