File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes how to solve numberformatexception which getting edittext field in android Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "how to solve numberformatexception which getting edittext field in android" Watch "how to solve numberformatexception which getting edittext field in android" New topic
Author

how to solve numberformatexception which getting edittext field in android

Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
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
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Feb 10, 2012
Posts: 213
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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.
Mohan Prasath
Ranch Hand

Joined: Jul 25, 2011
Posts: 38

Try to check for null and empty value in your "phone_number" object.


With regards,
Mohan Prasath
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
i caught the exception....the value it takes is numbers from 0-9.
how to get that ...??
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
Why is the code parsing the value after it has been found to be invalid?
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
actually ive put that in if else properly....
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Feb 10, 2012
Posts: 213
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
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Are you entering any float or double values in edittext?


Swastik
Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
iam not entering any float or double value......just numeric....
and have android:inputType:number


ANY IDEA WHY SUCH PROBLEM
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Feb 10, 2012
Posts: 213
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

Joined: Feb 10, 2012
Posts: 213
I solved it using
Long value=parseLong(myeditText.getText().toString());
shizuku Yakusa
Greenhorn

Joined: Apr 20, 2012
Posts: 1
if((edt.getText()+"") == ""){
// Write your code

} else {
// Write your code

}
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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 "==".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to solve numberformatexception which getting edittext field in android
 
Similar Threads
Keyboard events for EditText not reaching my event handler.
retrieve value from edittext from ListView
How to make TextVIew behave like EditText
storing a file
getText() help