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("");
}
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!
I'm having an issue when I get a whole number from an EditText and try to change that to a decimal so I can use it for calculations. Could someone explain how to do this?
For Example. if someone was to enter 120 into the EditText and I got the integer from it, how would I then change that integer of 120 into 1.20 and continue calculations with it?
Thanks!!
EditText myEditText = (EditText)findViewById(R.id.YOUR_EDIT_TEXT_ID);
String numberAsString = myEditText.getText().toString();
double myDecimal;
try {
myDecimal = Double.parseDouble(numberAsString);
if (myDecimal >= 10)
{
int digits = 1 + (int)Math.floor(Math.log10(myDecimal));
myDecimal = myDecimal / ((Math.pow((double)10, ((double)digits) - 1)));
System.out.println(myDecimal);
}
} catch (NumberFormatException e) {
//handle exeption
e.printStackTrace();
}
You need to get the contents from your EditText as a string, and from there you can parse it into an integer. This is done like so:
int wholeNum = Integer.parseInt(yourEditText.getText().toString());
int three = Integer.parse("3");
I know you can use this to parse a string into an integer, there is probably a parse method in double aswell!
Check this also :
Convert a String to Double - Java
+1 to anthony for answering 1 minute before me haha
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.
How to convert double to a string value that I get from the spinner, for example: "10 (ton)."
In summary:
How to convert string to double and delete the part (ton)?
I need only the value 10
Another option is using Java's substring method in the String class.
The signature is:
substring(int beginIndex, int endIndex)
Where endIndex equals to the index of the last character you want to include + 1.
In the case of your example, it will look like this:
String myString = "10 (ton)";
Double dbl = Double.parseDouble(myString.substring(0, 2));
Here is the link to the method:
Java substring
You must parse this String. Here is some example. Also use search.
Check these methods Double.parseDouble() and Double.toString() use these functions for converting double to string or vice-versa.
first you have to get rid of "(ton)" which can be achieved by using a String method for example
String inputString = "10 (ton)";
String str = inputString.split(" ")[0];
After that just parse the double Value
Double dbl = Double.parseDouble(str);
BTW: Not sure whether you want to go from double to string or vice-versa
i have a similar problem.
for correct formatting EditText text content to double value i use this code:
try {
String eAm = etAmount.getText().toString();
DecimalFormat dF = new DecimalFormat("0.00");
Number num = dF.parse(eAm);
mPayContext.amount = num.doubleValue();
} catch (Exception e) {
mPayContext.amount = 0.0d;
}
this is independet from current phone locale and return correct double value.
hope it's help;
My app has a number of numeric user input fields which need sanity checks before proceeding to the next intent.
I read viewText fields, convert them to double and then do the (numeric) tests but odd things happen and I find that while the code runs on my HTC in debug, it falls over if I publish then download the published version. My code is sumarised as;
String sFy;
double mFy=0;D
sFy=(txtFy.getText().toString());
mFy=Double.parseDouble(sFy);
if sFy is null the .parsedouble crashes. If I use;
sFy=(txtFy.getText().toString());
mFy=getDouble(sFy);
private double getDouble(String string){
double temp=0.0;
try {
temp = Double.parseDouble(string.trim());
} catch(NumberFormatException nfe) {
System.out.println("getDouble, Could not parse " + nfe);
}
return temp;
}
it works, even if sFy is empty.
Can anyone tell my why, or suggest a 'correct' method?
Maybe like that :
String sFy;
double mFy=0;
sFy = txtFy.getText();
if ((sFy != ""){
mFy=Double.parseDouble(sFy);
}
Or maybe I haven't really understood your problem...
Your getDouble is returning 0.0 in case there is NumberFormatException.
Do you see debugger coming to System.out.println("getDouble, Could not parse " + nfe);