0

In Java, I'm getting this Exception:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at com.reading.text.Activity3.readFile(Activity3.java:22)
    at com.reading.text.Activity3.main(Activity3.java:10)

From this Java code:

public static void main(String args[])
{
    readFile("C:/Users/forsakendoll/Desktop/boom.txt");
}

public static void readFile(String path) {
    Scanner file = null;
    try {
        file = new Scanner(new File (path));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        while (file.hasNext()) {
            for(int counter = 0 ; counter < file.next().length(); counter ++) {
                System.out.println(file.next().charAt(counter));    
            }
        }
    }
}

The Exception is thrown on

System.out.println(file.next().charAt(counter));

What does the Exception mean?

Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
user1708134
  • 585
  • 1
  • 8
  • 21
  • In addition to the below answers, you shouldn't have the code to read the file in the finally block, currently even if the file is not found the program will still attempt to read it. You should move code from the finally inside the try block. – BruteForce Apr 24 '13 at 14:45
  • @BruteForce thanks really bro. I did really stupid. I didn't realize that. – user1708134 Apr 24 '13 at 14:49

3 Answers3

3

You are calling .next() twice on every iteration of the loop, so when you are near the end, you jump off the end of the list and the compilter tells you there is nothing there.

Instead of this:

for(int counter = 0 ; counter < file.next().length(); counter ++) {
    System.out.println(`file.next()`.charAt(counter));    
}

Do this instead:

String temp = file.next();
for(int counter = 0 ; counter < next.length(); counter ++) {
    System.out.println(temp .charAt(counter));    
}  

SEE HERE

Community
  • 1
  • 1
samba
  • 2,143
  • 2
  • 11
  • 12
1

Don't call next() so much times! It actually go to the next element when you call it. If you need to use it more than once, put it inside a variable and use it.

        String next = file.next();
        for(int counter = 0 ; counter < next.length(); counter ++) {
            System.out.println(next.charAt(counter));    
        }
BobTheBuilder
  • 17,650
  • 5
  • 35
  • 58
0

The Scanner.next() method will move the internal iterator along one. Your code should be:

public static void readFile(String path) {
    Scanner file = null;
    try {
        file = new Scanner(new File (path));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        while (file.hasNext()) {
            String next = file.next();
            for(int counter = 0 ; counter < next.length(); counter ++) {
                System.out.println(next.charAt(counter));    
            }
        }
    }
}
eldris
  • 215
  • 1
  • 5
  • Meant to include the doc link: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next() Although the doc doesn't specifically mention this behaviour – eldris Apr 24 '13 at 14:40