0

i'm new here and also coding, i'm doing my course conclusion project and having difficulties to find where is the error. Basically my code are:

kilograms = Double.parseDouble(kgTextField.getText());
meters = Double.parseDouble(metersTextField.getText());
pounds = Double.parseDouble(poundsTextField.getText());
inches = Double.parseDouble(inchesTextField.getText());
result = (kilograms) / (meters * meters);    
resultTextField.setText(String.format("%,.2f", result ));

The result is giving me a NaN value and i'm not finding where is the error. I'm doing the conversion from string to double in the variables but still doesn't work. Can someone give me a light?

Cheers

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30

3 Answers3

0

"NaN" stands for "not a number". It is produced if a floating point operation has some input parameters that cause the operation to produce some undefined result. For example, 0.0 divided by 0.0 is arithmetically undefined. Taking the square root of a negative number is also undefined.

quote taken from this question

In your case, you have to check if the values obtained from the text fields are valid and treat invalid values accordingly.

ex. meters must be != 0

the code chunk below is taken from java.lang.Double and illustrates the possible results of dividing a double value by 0.0

 /**
 * A constant holding the positive infinity of type
 * {@code double}. It is equal to the value returned by
 * {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
 */
public static final double POSITIVE_INFINITY = 1.0 / 0.0;

/**
 * A constant holding the negative infinity of type
 * {@code double}. It is equal to the value returned by
 * {@code Double.longBitsToDouble(0xfff0000000000000L)}.
 */
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;

/**
 * A constant holding a Not-a-Number (NaN) value of type
 * {@code double}. It is equivalent to the value returned by
 * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
 */
public static final double NaN = 0.0d / 0.0;
Lucas Oliveira
  • 3,357
  • 1
  • 16
  • 20
0

thx for your answers and i'm sorry for didn't had posted cod enough to identify the error, as i told i'm newbie yet hehehe

My error was that i insert the variables in another section, so the system was identifying my variables as "0.00" so return to me a NaN error.

It's fixed now.

Thanks again!

0

JTextField converted to Double returning NaN

Cast conversions are well-defined. You don't have to guess, just check the JLS. (int,float & long) to double is a widening conversion.

private double result;

  private double result;
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    double kilograms = 0.0;
    double meters = 0.0;
    if (meters > 0) {
        result = (kilograms) / (meters * meters);
    } else {
        result = 0.0; //As you wish to store except NaN
    }
    jTextField1.setText("" + result);
    } 
                                

Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode.

Cheers

Community
  • 1
  • 1