2014-12-03 51 views
0

我看過關於此異常的其他問題,但似乎常見的問題是掃描程序過早關閉,但在此情況並非如此。這裏是我的代碼,這個問題線以上評論:掃描程序在調用next()時拋出NoSuchElementException()

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); 

} 

這是我收到的例外:

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) 

我想不出什麼會在這裏是問題。有人可以幫忙嗎?

+0

您的'Scanner'對象可能沒有包含try/catch中指定的值。在你的while循環之前做一個'System.out.println(scanner.hasNext());'來測試。如果它是「真的」,那麼你知道問題在哪裏。 – 2014-12-03 16:21:06

+0

我意外地從前一次運行中提供了錯誤的異常消息。我在我的問題中修改了它。 – Bobazonski 2014-12-03 16:23:52

+0

它看起來像你的文件以空行結束,它不包含導致'next()'拋出'NoSuchElementException'的任何數據。 – Pshemo 2014-12-03 16:24:31

回答

0

在掃描儀查找標記時,默認情況下使用空格。當它在特定的讀取中找不到標記時,它將繼續從源讀取所有數據,直到它完成。從Scanner.next

源()

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

所以考慮到這一點確保您使用空格來定義您的文件標記。

相關問題