numberFormatException - android

Is this sequence correct in android? I am tring to get EditText value and convert it into integer.
startTime_hour_int=Integer.parseInt(startTime_hour_edittext.getEditableText().toString());
Because my logcat throws the below error.
05-12 10:26:35.536: ERROR/AndroidRuntime(293): java.lang.NumberFormatException: unable to parse '' as integer
Can anyone tell me, how to solve this?

#Andro_Selva
If textbox startTime_hour_edittext is blank then Integer.parseInt is trying to parse "" into integer thats why you are getting NumberFormatException
so before use of startTime_hour_int=Integer.parseInt(startTime_hour_edittext.getEditableText().toString());
check condition
if(!startTime_hour_edittext.getText().toString().equalsIgnoreCase("")) {
startTime_hour_int=Integer.parseInt(startTime_hour_edittext.getEditableText().toString());
}

Do you call this code from some kind of listener on the EditBox? This can happen when you delete all content from the box. Just check that the text is not empty before parsing it to int.

Related

Calculator final output issue

I am creating a calculator app in android. It should calculate anonymously whatever is on textBox. For e.g. I enter 1+2.5-5*8 in textBox. But when I call addition method the app gets crashed. Because the input is in string format and answer I want is in numeric format. I used string buffer. I tried in java that when I enter (1+1+3-1) in stringBuffer and I display using println() method it give correct answer but same does not happen with string buffer when I take that value from editText.
You have to convert string into integer or float.
Use Integer.parseInt("") method to covert to integer and Float.parseFloat("") to convert to float.
First of all welcome to StackOverFlow
It might help if you add code and the error log in the question
So I am gonna cover all the possible things that might be happening wrong in your program:
NumberFormatException- you might not be converting the string "12" into int 12 to do so use Interger.parseInt(your string buffer with number only here);
Try using a stack and a queue for your solution (google it you will get details)
You might be making a mistake in getting string from edittext it sometimes give NullPointerException
if above is not enough comment and add your code asap

jsonschema2pojo not generating class

With the json response I am not getting the jsonschema2pojo class.
I am getting error like this "There's a problem: Unexpected character ('r' (code 114)): was expecting double-quote to start field name (line 2, column 2)".
I am newer in retrofit.Please help me.Thanks in advance.
Finally I got the solution that you need to put "" to all of your response parameter.

JSONException to parse HTML formatted text

I'm calling a web service from my Android application. The web service returns the following JSON :
Now the problem is when I'm trying to get the description element of the record object I'm getting an exception as org.json.JSONException: No value for description
Now my question is how can I get the value of the description using the existing parser without changing the response JSON format.
According to me,
You are using the wrong object to get description value otherwise org.json.JSONException: No value for description error will not occur.
Also, check if you can using optString instead of getString which just returns null if value do not exist, instead of throwing an exception.

Run time errors in eclipse android application [unfortunately application has stopped]

I am facing an issue in my app ,when I click the button inside my app it gives the error
(unfortunately application has stopped),
and these are problems in Logcat :
please help me out ,what exactly is the issue ?
i solved it , thnx guys
You have an editText which accepts inputs other than numbers also.
I assume you are trying to convert the value in the editText to an Integer using
Integer.parseInt ((String) EditText.getText());
Either restrict the input in the editText to numbers only.
Or, Handle Number format Exception gracefully with an appropriate error message!
It looks like you're trying to parse a value starting "android.widget.EditText" - which suggests you may have code like this:
int x = Integer.parseInt(editText.toString());
That's not going to get the text of the EditText - for that, you want to call getText():
int x = Integer.parseInt(editText.getText().toString());

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?
Try putting the Type to ' long ' instead of ' int '. Hope that will work for you.
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);

Categories

Resources