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.
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 <= index; i++){
System.out.print ("Enter polynomial degree " + i + ": ");
p.setCoefficient(i, key.nextDouble());
}
System.out.println(p);
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"));
}
} |