0

I've taken a look at other questions about this exception, but it seems that the common problem is that the scanner is closed too soon, which could not be the case here. Here is my code, with a comment above the problem line:

    public void windowOpened(WindowEvent arg0) {

    Scanner input = null;

    try {
        input = new Scanner(new File("/home/brian/workspace/Color Sampler/src/Data.txt"));
    } catch (FileNotFoundException e) {
        System.exit(1);
    }

    int i = 0;
    int nR, nG, nB;
    String nName;
    while(input.hasNextLine())
    {
        // These lines are throwing the exception
        nName = input.next();
        nR = input.nextInt();
        nG = input.nextInt();
        nB = input.nextInt();

        ColorSampler.colors[i] = new myColor(nName, nR, nG, nB); 

        i++;
    }

    ColorSampler.currentColor = ColorSampler.colors[0];
    System.out.println(ColorSampler.currentColor.red);

}

Here is the exception I am getting:

Exception in thread "AWT-EventQueue-1" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:855)
at java.util.Scanner.next(Scanner.java:1364)
at WindowDestroyer.windowOpened(WindowDestroyer.java:57)
at java.awt.Window.processWindowEvent(Window.java:1972)
at javax.swing.JFrame.processWindowEvent(JFrame.java:290)
at java.awt.Window.processEvent(Window.java:1933)
at java.awt.Component.dispatchEventImpl(Component.java:4649)
at java.awt.Container.dispatchEventImpl(Container.java:2103)
at java.awt.Window.dispatchEventImpl(Window.java:2588)
at java.awt.Component.dispatchEvent(Component.java:4475)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:675)
at java.awt.EventQueue.access$300(EventQueue.java:96)
at java.awt.EventQueue$2.run(EventQueue.java:634)
at java.awt.EventQueue$2.run(EventQueue.java:632)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:108)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:119)
at java.awt.EventQueue$3.run(EventQueue.java:648)
at java.awt.EventQueue$3.run(EventQueue.java:646)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:108)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:645)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

I cannot figure out what would be the problem here. Can someone help?

Bobazonski
  • 1,405
  • 5
  • 23
  • 39
  • Your `Scanner` object likely isn't carrying over the value assigned in your try/catch. Do a `System.out.println(scanner.hasNext());` before your while loop to test. If it's `true`, then you know where the issue is. – Drew Kennedy Dec 03 '14 at 16:21
  • I accidentally provided the wrong exception message from a previous run. I Revised it in my question. – Bobazonski Dec 03 '14 at 16:23
  • It looks like your file ends with empty line, which doesn't contain any data which causes `next()` to throw `NoSuchElementException`. – Pshemo Dec 03 '14 at 16:24
  • There is no empty line at the end of the file. The last line is formatted the same as the others. It also does not get through even one iteration of the loop, indicating that the issue is somewhere else. – Bobazonski Dec 03 '14 at 16:27

1 Answers1

0

In the Scanner finds tokens, by default, using whitespace. When it cannot find a token on a particular read it will continue to read all the data from the source until it does.

Source from Scanner.next()

while (true) {
    String token = getCompleteTokenInBuffer(null);
    if (token != null) {
        matchValid = true;
        skipped = false;
        return token;
    }
    if (needInput)
        readInput();
    else
        throwFor();
}

So with this in mind make sure your are using whitespace to define tokens in your file.

BrendanM
  • 363
  • 2
  • 13