Computer Science Canada java.lang.NumberFormatException: empty String? |
Author: | a22asin [ Wed Jan 22, 2014 6:35 pm ] |
Post subject: | java.lang.NumberFormatException: empty String? |
Hey, ok so i keep getting this error for my program when asking the user for the value of x: Exception in thread "main" java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at question2.PolynomialTest.main(PolynomialTest.java:25) coming from this part of my code: String x; do{ System.out.print ("Enter a value of x for which to evaluate the polynomial: "); x = key.nextLine(); if (!x.equals("quit")){ double in = Double.parseDouble(x); double result = p.evaluate(in); System.out.println ("For x = " + x +", polynomial =" + result); } }while(!x.equals("quit")); The Program will run right till it asks for an input for x, it wont let me input a value, it just gives the error. I tried initializing x as a random string at the start since the error says "empty string", i tried making "quit" a variable and comparing it to x instead. But nothing seems to work and i cant figure out why im getting this. Any ideas? oh and line 25 would be when i parse the double. |
Author: | DemonWasp [ Wed Jan 22, 2014 10:26 pm ] |
Post subject: | RE:java.lang.NumberFormatException: empty String? |
"Empty string" is different from "null string". Empty means "no content", null means "no value". So after x = key.nextLine(); , x isn't null, it's just empty. Without seeing how 'key' is set up, I can't tell you anything more about why that would be the case (don't forget: the 'nextLine()' method will return an empty string if it encounters an extra line-break in its input). |
Author: | a22asin [ Wed Jan 22, 2014 10:34 pm ] | ||
Post subject: | Re: RE:java.lang.NumberFormatException: empty String? | ||
DemonWasp @ Wed Jan 22, 2014 10:26 pm wrote: "Empty string" is different from "null string". Empty means "no content", null means "no value".
So after x = key.nextLine(); , x isn't null, it's just empty. Without seeing how 'key' is set up, I can't tell you anything more about why that would be the case (don't forget: the 'nextLine()' method will return an empty string if it encounters an extra line-break in its input). Well the error occurs before i have a chance to enter a value for x. The key is the scanner object and works ealier in the code, so thats why i dont get whats wrong. Heres my entire code as maybe something may have been wrong in between.
|
Author: | Tony [ Wed Jan 22, 2014 11:21 pm ] | ||||
Post subject: | RE:java.lang.NumberFormatException: empty String? | ||||
Think about the difference between
and
hint: read the documentation. http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextInt() http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextLine() |