Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to solve numberformatexception which getting edittext field in android  RSS feed

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i have a edittext field which takes numeric oly.
i want to insert that value into database..
iam trying to do something like this



and it gives numberformat exception....unable to parse
 
Saloon Keeper
Posts: 4280
106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like the contents of the field is nor parseable into a number. Which value does it have? you should catch the exception regardless, as you don't want the app to crash just because someone accidentally typed a non-number.
 
Suzzane Pinto
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi;


i have written android:inputType="number" in xml.it alows to take numbers only....
and i want to insert it into the database to a numeric field only
 
Tim Moores
Saloon Keeper
Posts: 4280
106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Which value does it have?



Tim Moores wrote:You should catch the exception regardless, as you don't want the app to crash just because someone accidentally typed a non-number.



An empty field would also be a non-number.
 
Ranch Hand
Posts: 38
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to check for null and empty value in your "phone_number" object.
 
Suzzane Pinto
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i caught the exception....the value it takes is numbers from 0-9.
how to get that ...??
 
Suzzane Pinto
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final EditText phone_number = ( EditText) findViewById(R.id.editText1);

if(phone_number.getText().toString().length()==0)
{
phone_number.setError( "Mobile number is required!" );
}
myEditValue1 = phone_number.getText().toString();

myEditValue2=Integer.valueOf(myEditValue1);

IT SAYS UNABLE TO PARSE AS INTEGER

PLEASE SUGGEST



 
Tim Moores
Saloon Keeper
Posts: 4280
106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the code parsing the value after it has been found to be invalid?
 
Suzzane Pinto
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually ive put that in if else properly....
 
Tim Moores
Saloon Keeper
Posts: 4280
106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't mislead us by posting code that is not the code that you're using; that just makes it harder to help you.
 
Suzzane Pinto
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dnt knw why it is not parsing here....
iam entering number using getText().toString() ...now iits a string agn if i parse to integer...i think dats why it gives cannot parse and numberformatexception
is there any way to get the number entered as integer and pass directly...
please suggest
 
Rancher
Posts: 1830
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you entering any float or double values in edittext?
 
Suzzane Pinto
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iam not entering any float or double value......just numeric....
and have android:inputType:number


ANY IDEA WHY SUCH PROBLEM
 
Tim Moores
Saloon Keeper
Posts: 4280
106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this was my problem I'd write the value that's passed to Integer.parseInt to the logcat output, so that I wouldn't have to guess what's going wrong.
 
Suzzane Pinto
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i printed on log the value i pass to Integer.parseInt is getting....but after that it goes to catch numberformatException
 
Suzzane Pinto
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved it using
Long value=parseLong(myeditText.getText().toString());
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if((edt.getText()+"") == ""){
// Write your code

} else {
// Write your code

}
 
Tim Moores
Saloon Keeper
Posts: 4280
106
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shizuku Yakusa wrote:if((edt.getText()+"") == "")


Very, very wrong. Read up on the difference between string equality and object equality. What the "==" does here is exactly the wrong one - don't compare strings using "==".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!