String <> Double; JTextfield
Hi,
I have a JTextfield and would like to compare the entered text or number with a certain result:
publicvoid actionPerformed(ActionEvent arg0){
JTextfieldString = solutionTextfield.getText();
System.out.println(PMgmt.calculateDistance * 60) / PMgmt.calculateSpeed);
if (JTextfieldString.equals((PMgmt.calculateDistance * 60) / PMgmt.calculateSpeed)){
System.out.println("OK");
(...)
The methods "calculateDistance" and "calculateSpeed" return a double value. If I enter the exact value I see from the System.out.println call it's still not correct. I guess it's due to how I compare the String value JTextfieldString with the result of the methods in double format. I tried a ".toString()" but I wasn't successful.
Thanks for your help!
[981 byte] By [
SFLa] at [2007-11-27 9:54:17]

First things first get the text from the text field and try to convert it to a double double d = Double.parseDouble(JTextfieldString) ;
. If it throws a NumberFormatException you don't have a number. (For example they entered "Fred" instead of 1.00797. Otherwise you can plunk it into your calculations and move on your merry little way.
PS.
Thanks a lot! :)One more question: it's enough to show 2 decimal places; could I work with java.text.NumberFormat or is there another method just to calculate / show / evaluate 2 decimal places?
SFLa at 2007-7-13 0:24:09 >

> Thanks a lot! :)
>
> One more question: it's enough to show 2 decimal
> places; could I work with java.text.NumberFormat or
> is there another method just to calculate / show /
> evaluate 2 decimal places?
Use DecimalFormat instead:
http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html
Thanks! I can now format Strings without problems ;)However: could I format a double value?
SFLa at 2007-7-13 0:24:09 >

double sTime = ((PMgmt.calculateDistance * 60) / PMgmt.calculateSpeed);
double somedata = decFormat.format(sTime);// error
Error = "Type mismatch: cannot convert from String to double"
My 2 methods calculateDistance and calculateSpeed return a double value.
SFLa at 2007-7-13 0:24:09 >

> > double sTime = ((PMgmt.calculateDistance * 60) /
> PMgmt.calculateSpeed);
> double somedata = decFormat.format(sTime);//
> error
>
>
> Error = "Type mismatch: cannot convert from String to
> double"
>
> My 2 methods calculateDistance and calculateSpeed
> return a double value.
import java.text.DecimalFormat;
DecimalFormat fmt=new DecimalFormat("0.##");
double display=x*y;
System.out.print("Decimal Format is : "+fmt.format(display));
...hmmm... I need to store the formated value in a variable to compare it to another one... I also tried the following:if (userInput == fmt.format(sTime)) {...which doesn't work...?
SFLa at 2007-7-13 0:24:09 >

> ...hmmm... I need to store the formated value in a
> variable to compare it to another one... I also tried
> the following:
> > if (userInput == fmt.format(sTime)) {
>
> ...which doesn't work...?
U meant compare String to Double value? Or what?
If you want to compare the user's input (which is String), first you have to convert the String to the same type of the value(Double, Integer etc) u want to compare to
Sure, both variables are of the same type: doubleI would like to format "sTime" so that it shows just 2 decimal places and compare it to "userInput"; that's why I thought I can use DecimalFormat... Is there another way to truncate decimal places?
SFLa at 2007-7-13 0:24:09 >

> Sure, both variables are of the same type: double
>
> I would like to format "sTime" so that it shows just
> 2 decimal places and compare it to "userInput";
> that's why I thought I can use DecimalFormat...
>
> Is there another way to truncate decimal places?
If I am not wrong so in your previous post u just compare String to Double value which is illegal.
U can convert the userInput to Double first
Then u format it into DecimalFormat
You also have to format the value that u want to compare to into Double and DecimalFormat
Then u compare.