自从我醒来以来,我一直试图让这个工作起作用,我根本无法弄清楚是什么导致了这个异常。
try {
scan = new Scanner(new File("dictionary.txt"));
} catch (FileNotFoundException ex) {
System.out.print("File not found");
}
ArrayList<String> dictionary = new ArrayList<String>();
while (scan.hasNextLine()) {
System.out.println(scan.next());
dictionary.add(scan.nextLine());
}
scan.close();
这是输出,第167行是“dictionary.add(scan.nextLine)”:
C:\用户\ Aaronbobaron \文件\的NetBeansProjects \ W
鳄鱼
斑马
袋鼠
冰
雪
山猫
摩天大楼
线程“AWT-EventQueue-1”中的异常java.util.NoSuchElementException:找不到行 at java.util.Scanner.nextLine(Scanner.java:1585) 在Word.jButton1ActionPerformed(Word.java:167) 在Word.access $ 100(Word.java:16) 在Word $ 2.actionPerformed(Word.java:92) 在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) 在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2341) 在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 在java.awt.Component.processMouseEvent(Component.java:6505) 在javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729) at java.awt.EventQueue.access $ 200(EventQueue.java:103) at java.awt.EventQueue $ 3.run(EventQueue.java:688) at java.awt.EventQueue $ 3.run(EventQueue.java:686) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue $ 4.run(EventQueue.java:702) at java.awt.EventQueue $ 4.run(EventQueue.java:700) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:699) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
文本文件是:
鳄鱼
斑马
kangaroo
ice
下雪
bobcat
摩天大楼
答案 0 :(得分:1)
这两个都转移到下一个标记:
System.out.println(scan.next());
dictionary.add(scan.nextLine());
要解决此错误,请在致电第二行之前再次检查:
while (scan.hasNextLine()) {
System.out.println(scan.next());
if (scan.hasNextLine())
dictionary.add(scan.nextLine());
}
答案 1 :(得分:1)
while (scan.hasNextLine()) {
System.out.println(scan.next());
dictionary.add(scan.nextLine());
}
您检查一次是否有更多内容,然后您拨打next()
两次的表单。无法保证第二次通话不会失败(如您所知)。
答案 2 :(得分:1)
我猜你试图跳过代码中的空行。
这应该可以正常工作。将scan.next中的值转换为临时字符串变量,并使用相同的内容进行打印以及添加到字典中。
String tmp;
while (scan.hasNextLine()) {
tmp = scan.next();
System.out.println(tmp);
dictionary.add(tmp);
}
scan.close();