I am trying to parse a double from an edittext but i keep getting a number format exception error.
i initially tried this...
premiumvalue = Double.parseDouble(premium.getText().toString());
amountweightvalue = Double.parseDouble(amountweight.getText().toString());
Then attempted this...to no avail.
try{
premiumvalue = Double.parseDouble(premium.getText().toString());
} catch (final NumberFormatException e) {
premiumvalue = 0;
}
try{
amountweightvalue = Double.parseDouble(amountweight.getText().toString());
} catch (final NumberFormatException e) {
premiumvalue = 0;
}
I'm a noob and any help fixing this would be greatly appreciated.
I have test your code test your code myself and find the following possibility to find NumberFormatException:
if EditText value is blank, So apply condition for EditText blank value.
if EditText value is only dot(.), So apply condition for EditText dot value.
in your comment you are saying that you are using this
String text = premium.getText().toString();
if(text.equals(".")||text.equals("-.")||text.equals(""))
{
premium.setText("0");
}
so my point is that
are you using same thing for both premium and amountweight EditText and then parse string to double??
and why you need to check condition for '_' because if you set android:inputType="numberDecimal" attribute for EditText then EditText only accept 0-9 and . value.
you don't need to trim EditText value bacuse Edittext do not accept blank space if you are using android:inputType="numberDecimal" attribute.
don't need to check null condition because EditText never return null value.
I have test your code lot of way and do not find NumberFormatException if i follow above points.
Related
I have a android method that converts degrees minutes and seconds to decimal. I am getting the text from edittext split it and convert it to double array before I pass it to the method. Then I wanted to get the double returned in a decimal form to be displayed in the original edittext as string. Here is the code,
public double DegreeToDecimal(double d, double m, double s)
{
double decimal;
decimal = d + m/60 + s/3600;
return decimal;
}
try
{
String string = dtod.getText().toString();
String[] s = string.split(":");
String decimal;
double d = DegreeToDecimal(Double.parseDouble(s[0]), Double.parseDouble(s[1]), Double.parseDouble(s[2]));
decimal = String.valueOf(d);
dtod.setText(decimal);
}catch (Exception e){
e.printStackTrace();
}
When I do this on a button click, nothing happens. The Logcat doesn't show anything and the code is simply ignored. What am I doing wrong?
Thanks for the help.
Your code formatting is all over the place and hard to read, please try to use correct indentation next time!
From the code you posted it's hard to tell what happens. If Logcat doesn't show any stacktraces like you said, and if your code is "ignored" as you say then my guess is that it doesn't get called.
Did you attach an OnClickListener to that button?
Otherwise try to log something between your lines or use a debugger to find out what really happens!
So first of all sorry if this has already been asked and answered before, I couldn't find anything relating to my issue.
So I'm working on a project for college and I need to get int values from EditText widgets. I was told to use parseInt to do this however when running my program, that line of code causes the application to crash. I don't know what I'm doing wrong, I'm still very new to android development, thanks for the help :)
public void Calculate (View view)
{
int MilesTravelled;
int FuelUsed;
int MPG;
/* the two lines below are what cause the application to crash */
MilesTravelled = Integer.parseInt(txtMilesTravelled.getText().toString());
FuelUsed = Integer.parseInt(txtFuelUsed.getText().toString());
FuelUsed = (int) (FuelUsed / 4.55);
MPG = MilesTravelled / FuelUsed;
lblMPG.setText(FuelUsed);
}
Do you have this in the onCreate() function?
EditText txtMilesTravelled = (EditText) findViewById(R.id.YourEditText);
But I think you mixed Integer and int. They are not the same:
See this link!
First of all, don't capitalize the first letter of an variables or method names. Following the Java coding conventions, only do that for classes.
What is probably causing your app to crash is you trying to set the text of a label to an integer. The setText method for a TextView needs to take in a string.
So change:
lblMPG.setText(FuelUsed);
to:
lblMPG.setText(String.valueOf(FuelUsed));
Otherwise it might be that it's trying to parse a non-numerical string to an integer.
For exmaple, if the EditText is blank, it will cause your app to crash. To prevent that, try this:
int MilesTravelled = 0, FuelUsed = 0;
try {
MilesTravelled = Integer.parseInt(txtMilesTravelled.getText().toString());
FuelUsed = Integer.parseInt(txtFuelUsed.getText().toString());
} catch (NumberFormatException nfe) {
Toast.makeText(getApplicationContext(), "Error NFE!", 0).show();
nfe.printStackTrace();
}
This way, it will catch a NumberFormatException error (parsing a string to an integer that can't be represented as an integer, such as "hello"). If it catches the error, it will toast that an error has occurred and your integer variables will remain 0.
Or you could just test if the strings contain only digits using the following regex:
int MilesTravelled = 0, FuelUsed = 0;
if (txtMilesTravelled.getText().toString().matches("[0-9]+")) {
MilesTravelled = Integer.parseInt(txtMilesTravelled.getText().toString());
} else {
// contains characters that are not digits
}
if (txtFuelUsed.getText().toString().matches("[0-9]+")) {
FuelUsed = Integer.parseInt(txtFuelUsed.getText().toString());
} else {
// contains characters that are not digits
}
If that's not the problem, then make sure you define your variables properly.
txtMilesTravelled and txtFuelUsed should be EditText:
EditText txtMilesTravelled = (EditText)findViewById(R.id.txtMilesTravelled);
EditText txtFuelUsed = (EditText)findViewById(R.id.txtFuelUsed);
And make sure that your R.id.editText actually exists on your layout and that the IDs are the correct ones.
Last thing, make sure FuelUsed is not 0 before calculating MPG because then you are dividing by 0:
int MPG = 0;
if (FuelUsed != 0) {
MPG = MilesTravelled / FuelUsed;
}
I am assuming that you're entering perfect integers in the EditTexts. It might be a good idea to use the trim function txtMilesTravelled.getText().toString().trim() before using parseInt.
However, I think the major problem is here : lblMPG.setText(FuelUsed);
FuelUsed is an integral value, when you pass an integer to setText(), it looks for a string resource with that integral value. So you should be passing a String to the setText() method.
Use : lblMPG.setText(Integer.toString(FuelUsed));
I have written a calculator type app. My mates found that entering single decimal points only into the editText's makes the app crash. Decimal numbers and integers work fine, but I get a number format exception when .'s are entered.
I want to check if a single . has been placed in an editText, in order for me to display a toast telling the user to stop trying to crash the app.
My issue is that a . doesn't have a numerical value...
You can wrap it in a try/catch which should be done anyway when parsing text. So something like
try
{
int someInt = Integer.parseInt(et.getText().toString());
// other code
}
catch (NumberFormatException e)
{
// notify user with Toast, alert, etc...
}
This way it will protect against any number format exception and will make the code more reusable later on.
You can treat .1 as 0.1 by the following.
String text = et.getText().toString();
int len = text.length();
// Do noting if edit text just contains a "." without numbers
if(len==0 || (len==1 && text.charAt(0).equals(".")))
return;
if(text.charAt(0).equals(".") && text.length() > 1) {
text = "0" + text;
}
// Do your parsing and calculations
I am building an simple Android app am looking for a way to compare number input to a pre-stored integer. My first though was:
if(R.id.number == 123456){
}
This comparison does not work. I have also tried .equals, with no avail. Does anyone have any thoughts on how to compare the two values?
The R.id.number refers to a View's id (most likely an EditText since you say you are comparing user input). Thus, comparing that to a number would definitely not be what you're looking for. Find the EditText via findViewById(), parse its text into an integer and compare that.
Eg
public void onCreate (Bundle b){
super.onCreate (b);
EditText e = (EditText) findViewById (R.id.number);
int num = 0;
try{
num = Integer.parseInt (e.getText().toString().trim());
}
catch (NumberFormatException e){
}
if (num == 123456){
System.out.println ("Input equal");
}
}
Im using this code to get value from a string and then posting it after performing operation.
The problem is that after entering some value when I try to erase the entire text field it stops unexpectedly.Im using onTextChanged method and the piece of code that Im working with is here. Also it works fine when I use a Button to submit instead on textWatcher.
min=(EditText) findViewById(R.id.minutes);
sec=(EditText) findViewById(R.id.seconds);
millisec=(EditText) findViewById(R.id.milliseconds);
String minute = min.getText().toString();
Double min1=Double.parseDouble(minute);
min1=min1*60;
Double min2=min1*1000;
sec.setText(Double.toString(min1));
millisec.setText(Double.toString(min2));
parseDouble will throw a NumberFormatException if you do not pass a parsable String...
So if you empty the field, you will get an exception.
You can wrap your code in a try-catch-clause:
String minute = min.getText().toString();
try{
Double min1 = Double.parseDouble(
min1=min1*60;
Double min2=min1*1000;
sec.setText(Double.toString(min1));
millisec.setText(Double.toString(min2));
} catch(NumberFormatException e){
sec.setText("");
millisec.setText("");
}