-1

My Scanner was working fine, reading lines in just dandy and now it is no longer working and continues to throw NoSuchElementException: No Line Found error.

My code:

        try {
            File file = new File("Word200D16.txt");
            Scanner scanner = new Scanner(file);
            while(scanner.hasNextLine()){
                for(int i = 0; i < 200; i++){
                    String line = scanner.nextLine();
                    elementsToAdd[i] = line;
                }
            }

        } catch(Exception er) {
            System.out.println("Error: " + er);
        }

Is there anything obviously wrong with this code that I am overlooking? I want each line to be saved into my string array elementsToAdd.

Jud
  • 1,324
  • 3
  • 24
  • 47
  • You're misusing the pattern. `hasNextLine` checks for one line. You then attempt to read 200 `nextLine`s`. 1 for sure vs 200 not so much. – Savior Apr 07 '16 at 15:44

1 Answers1

2

Is there anything obviously wrong with this code that I am overlooking?

Yes. You are checking hasNextLine once then calling nextLine 200 times in a loop without checking.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116