I am trying to read a serise of variables from a text file with this code:
public static Book readBook(String pathname) throws IOException, InputMismatchException {
Scanner fl = new Scanner (new FileInputStream (fn));
int num = fl.nextInt();
double thePrice = 0;
String theAuthor = null;
String theTitle = null;
for (int i=0; i<=num; i++) {
theTitle = fl.nextLine();
theAuthor = fl.nextLine();
thePrice = fl.nextDouble();
}
System.out.print(num);
System.out.print(theTitle);
System.out.print(theAuthor);
fl.close();
return new Book(theTitle, theAuthor, thePrice);
}
The File contains a number that is used in the while loop to state how many passes are needed. The File looks like this:
2
name
author
10.00
name2
author2
12.00
But this throws an input mismatch error that for some reason reads the first line as 'name2' messing up the order and causing the error when the code reaches the double. Any Help would be appreciated!
Stack Trace:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Model.readBook(Model.java:36)