I'm making a program that reads from a file 3 numbers, doubles or integers, per line except the first one. The first line is the the number of lines after. The 3 numbers in each line are variables a, b, and c in the quadratic formula. Then the program is supposed to return the difference between the + and - part of the formula. Here is an example of what the file may contain.
3
1 2 4
1.5 3.12 4.31
0.09 5 2
My problem is the when using scanner to turn the tokens in the file into doubles so that they can be inputted to the quadratic formula, I am getting the following error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1250)
at java.lang.Double.parseDouble(Double.java:540)
at gorf.main(gorf.java:23)
here is part of my code:
int lines = Integer.parseInt(sc.nextLine());
double a;
double b;
double c;
String line = "";
Scanner abc = new Scanner(line);
for(int count = 0; count < lines; count++)
{
line = sc.nextLine();
abc = new Scanner(line);
a = Double.parseDouble(abc.next());
b = Double.parseDouble(abc.next());
c = Double.parseDouble(abc.next());
}
"3"
which was at start of your input). – Pshemo"1"
as a double should not be a problem. I think Pshemo's comment is still somewhat relevant in that there must be some non-visible character in there that is creating problems. Consider assigning the result ofScanner.next()
to a variable before parsing, and then examine each of the characters individually to figure out which odd character sneaked in there. – sstan"1"
here is the code I changed.str = abc.next(); System.out.println(str); a = Double.parseDouble(str);
Sorry for the formatting i can't get it to work for some reason. @sstan – Dane WilliamsstrVar.length()
, which I predict will return more than1
. Then you can examine each of the characters individually, something like:(int)strVar.charAt(n)
. – sstan