0

So I have my code below that is supposed to scan a file, and then print some data to another file. The problem is that it insists on throwing the NoSuchElementException, even though the file I'm reading from has data in it. It's more like the scanner starts at the end of the file, where there is no data. I tried using another file, and many other ways to fix it, but with no benefit:

public static void scanField(Scanner sc, PrintWriter pr) throws Exception {
    int n = sc.nextInt();
    int m = sc.nextInt();

    while (n != 0 && m != 0) {
        String[][] field = new String[n][m];
        for (int i = 0; i < n; i++) {

            **String temp = sc.next();**

            for (int j = 0; j < m; j++) {
                field[i][j] = "l";
            }
        }

The line between the asterisks is the one that causes the error. Quick assistance is appreciated. Thank you.

blm
  • 2,145
  • 2
  • 16
  • 21
  • Check [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods). – sam Oct 18 '15 at 20:24

1 Answers1

0
 while (n != 0 && m != 0) 

Your n and m values are immutable therefore you're going to infinite loop. Probably scanner reaches end of file and therefore throws mentioned exception.

Klever
  • 131
  • 7