
-----------------------------------
a22asin
Wed Jan 22, 2014 6:35 pm

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.

-----------------------------------
DemonWasp
Wed Jan 22, 2014 10:26 pm

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).

-----------------------------------
a22asin
Wed Jan 22, 2014 10:34 pm

Re: 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).

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.

[code]package question2;

import java.util.*;

public class PolynomialTest {
	public static void main(String[] args){

		Scanner key = new Scanner (System.in);	
			
		System.out.print ("Enter polynomial degree: ");
		int index = key.nextInt();
		Polynomial p = new Polynomial(index);
		
		for (int i = 0; i 