Java - Error de cálculo

I have wrote the following code to work out the difference between two floating point numbers to two decimal places:

double p = (Math.round((Double.parseDouble(a) - Double.parseDouble(a))*100))/100;

a y la b are values in a String array that are numbers with decimal places, but obviously in the format of a String. I've tried to convert the String numbers into doubles and carry out the calculation with them but it doesn't seem to be working and throws this error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "N/A"
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)

Additional info: This uses data from the web and seemed to work before, but now it seems to have a bug of sorts. How can I solve this, and what's the problem?

preguntado el 01 de febrero de 12 a las 14:02

What is the value of a ? -

Either 'a' or 'b' is equal to "N/A". This is a problem. -

Check if these strings use dot as delimiter and not comma -

3 Respuestas

El problema es que tu String las variables a y la b are not properly formatted - "N/A" is not a proper double.

Please post how you are getting them.

Respondido 01 Feb 12, 18:02

This has helped me identify the issue, and I'm working on it now. Thank you for pointing that out, don't know how I missed it! - mino

The root of your error is java.lang.NumberFormatException: For input string: "N/A"

Eso significa a or b tiene un valor de N/A.

Respondido 01 Feb 12, 18:02

There is no b in your code. If you are trying to do something like this:

String[] a = {"123","23"} ;
double p = (Math.round((Double.parseDouble(a[0]) - Double.parseDouble(a[1]))*100))/100;

then you might have a typo mistake. Because it prints out fine for me.

Respondido 01 Feb 12, 18:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.