Reputation: 25
i'm making a program that views a text file and prints it to the console in eclipse. One of the lines in the text file looks like this...
A.Matthews 4 7 3 10 14 50
when running the program, I get an error like this..
and this is the program
import java.io.*; // for File
import java.util.*; // for Scanner
public class MapleLeafLab {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("mapleleafscoring.txt"));
while (input.hasNextLine()) {
String line = input.nextLine();
Scanner lineScan = new Scanner(line);
String name = lineScan.next(); // e.g. "Eric"
String rest = lineScan.next();
int GP = lineScan.nextInt(); // e.g. 456
int G = lineScan.nextInt();
int A = lineScan.nextInt();
int P = lineScan.nextInt();
int S = lineScan.nextInt();
Double SS = lineScan.nextDouble();
System.out.println(name+rest+" "+GP+" "+G+" "+A+" "+P+" "+S+" "+SS);
//System.out.println(name + " (ID#" + id + ") worked " +
// sum + " hours (" + average + " hours/day)");
}
}
}
Upvotes: 0
Views: 51
Reputation: 121649
Here's the Javadoc for Scanner:
https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
public double nextDouble()
Scans the next token of the input as a double... If the next token matches the Float regular expression defined above then the token is converted into a double value...
Returns: the double scanned from the input Throws: InputMismatchException - if the next token does not match the Float regular expression, or is out of range NoSuchElementException - if the input is exhausted IllegalStateException - if this scanner is closed
You're getting NoSuchElementException
because you're trying to read 8 tokens from a 7-token line.
A.Matthews => name
4 => rest
7 => GP
3 => G
10 => A
14 => P
50 => S
SS => NoSuchElementException
Upvotes: 1