Reputation: 577
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
Reputation: 1179
Try putting the Type to ' long ' instead of ' int '. Hope that will work for you.
Upvotes: 5
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
Reputation: 128428
You need to put a simple condition:
if(mContactNumber.getText().length()>0)
{
int contactNumber=Integer.parseInt(mContactNumber.getText().toString().trim());
}
Upvotes: 2
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