0

I'm getting the following error output when trying to run my code.

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1416)
    at Olympics.main(Olympics.java:61)
Java Result: 1

The project includes a text file called olympics.txt and is a file of statistics of Olympic athletes with their countries etc. I have the number of lines in the file, lineNum, and am using this for loop to add each piece of information into corresponding arrays.

for(int j = 0; j <= lineNum-1; j++)
{
    String line = fileScan.nextLine();
    Scanner lineScan = new Scanner(line);

    lineScan.useDelimiter("\t");
    name[j] = lineScan.next();
    age[j] = lineScan.nextInt();
    country[j] = lineScan.next();
    year[j] = lineScan.next();

    String ceremony = lineScan.next();
    sport[j] = lineScan.next();

    int gold = lineScan.nextInt();
    int silver = lineScan.nextInt();
    int bronze = lineScan.nextInt();

    total[j] = lineScan.nextInt();
}    

Line 61 is the age[j] = lineScan.nextInt(); I commented the age line out to see if it worked then, but it still didn't work. Any help is appreciated, and if you need more code, then let me know.

Mauren
  • 1,955
  • 2
  • 18
  • 28
5antoro
  • 85
  • 1
  • 8

1 Answers1

1

As from the doc, useDelimiter(String) takes a regex, not an example of where you want to cut.
To split at tabs, you need to use useDelimiter("\\t").
That's why next() throws an exception, it's just unable to split the line with the delimiter you set.

Similar question

Community
  • 1
  • 1
Moinonime
  • 304
  • 1
  • 5
  • 12