1

Information :

when I execute my program, a simple file reader, I get an exception.

I have my file saved next to my .java files.

Output :

run:
name : Koen
score : 44
Exception in thread "main" java.util.NoSuchElementException: No line found
name : Kevin
score : 55
at java.util.Scanner.nextLine(Scanner.java:1585)
at tetris.FileIO.loadHighscores(FileIO.java:41)
at tetris.FileIO.getLineScores(FileIO.java:28)
at tetris.FileIO.main(FileIO.java:62)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Code:

package tetris;

import java.io.File;
import java.io.InputStream;
import java.io.Writer;
import java.util.Scanner;

public class FileIO   {
    private File file;
    private Scanner filescScanner, lineScanner;
    private Writer fileWriter, lineWriter;
    private String[][] data;

    public FileIO () {
        String[][] data = new String[100][1];
    }
    public String[][] getLineScores(){
        return this.loadHighscores(this.getClass().getResourceAsStream("LineHighscores.txt"));
    }
    public String[][] getTimeScores(){
        return this.loadHighscores(this.getClass().getResourceAsStream("TimeHighscores.txt"));
    }

    public String[][] loadHighscores( InputStream resourceStream){

        int x=0;
        String test = "";

        filescScanner = new Scanner(resourceStream);

        while((test=filescScanner.nextLine())!=null) {

            lineScanner = new Scanner(test);
            lineScanner.useDelimiter("-/-");
            System.out.println("name : "+lineScanner.next());
            System.out.println("score : " +lineScanner.next());

            x++;
        }
        lineScanner.close();
        filescScanner.close();
        return data;    
    }

    public static void main(String[] args){
        FileIO file = new FileIO();
        System.out.println(file.getLineScores());
    }
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Koen Demonie
  • 539
  • 1
  • 7
  • 24

2 Answers2

4

When you are calling nextLine():

 while((test=filescScanner.nextLine())!=null)

it is throwing an exception to indicate that the scanner has no more lines of input. Thus, it will never return null.

Try instead to use hasNextLine() to check if there is a next line.

while(filescScanner.hasNextLine())
{
    test=filescScanner.nextline();
    ...
}

hasNextLine

public boolean hasNextLine()

Returns: true if and only if this scanner has another line of input (source)

Therefore, when the scanner do not have any more lines of input to read, hasNextLine() will return false. Hence, terminating the cycle.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
0

If I understand you correctly you would like the while loop to terminate when where are no more lines left to read in the file. The Scanner class implements a hasNextLine method which returns true when there is a line to be read and false otherwise. I think you can replace the while condition with scanner.hasNextLine() and this will address the NoElementException that is being raised at line 41. I hope you find this answer helpful.

Good Luck Nikolai

NikA
  • 23
  • 2