2013-12-08 22 views
-1

我的方法运行,但在执行一次后会得到异常。我不明白为什么。第24行,在例外中注明的行是“choice = in.nextInt();”
运行此方法时获取NoSuchElementException

这里是我的例外:

1查找某一项目。
2显示所有项目。
3更新项目。
4将项目保存到磁盘。
5退出。
您选择从文件中找到一个项目。
输入您想要查找的dvd的sku。
857295 Star.Wars 152
1查找项目。
2显示所有项目。
3更新项目。
4将项目保存到磁盘。
5退出。在java.util.Scanner.throwFor(未知来源)
异常在线程 “主” java.util.NoSuchElementException

在java.util.Scanner.next(未知来源)
在java.util.Scanner中。 nextInt(来源不明)
在java.util.Scanner.nextInt(来源不明)
在version4.version4.main(version4.java:24)

这里是我的代码:

import java.util.*; 
import java.io.*; 

public class version4 
{ 
public static void main(String[] args) throws FileNotFoundException 
{ 
Scanner in = new Scanner(System.in); 
boolean exit = false; 
int choice = 0; 

while (!exit) 
{ 
    System.out.println("1 Find an item.\n2 Display all items.\n3 Update item.\n4 Save item to disk.\n5 Quit."); 
    choice = in.nextInt(); 
    switch (choice){ 

    case 1: System.out.println("You chose to find an item from file."); findItem(); break; 
    case 2: System.out.println("You chose to display all items."); readDisplay(); break; 
    case 3: System.out.println("You chose to update an item."); itemUpdate(); break; 
    case 4: System.out.println("You chose to save an item to disk."); itemAdd(); break; 
    case 5: exit = true; in.close(); break; 
    default: System.out.println("That is not a valid option."); break; 
} 
} 
System.out.println("Goodbye."); 
} 

public static void findItem() throws FileNotFoundException { 

    FileReader reader = new FileReader("read_record.txt"); 
    Scanner fin = new Scanner(reader); 
    String str = null; 
    ArrayList<String> dvdfile = new ArrayList<String>(); 
    if (fin != null && fin.hasNext()) { // do we have a scanner, is there anything to read? 
     while (fin.hasNext()) { // while there's something to read... 
     str = fin.next(); // read it. 
     if (str == null) { // end if it's null 
      break; 
     } 
     dvdfile.add(str); // otherwise add it. 
     } 
    } fin.close(); 

    Scanner kb = new Scanner(System.in); 
    System.out.println("Enter the sku of the dvd you wish to find."); 
    String dvdSku = kb.next(); 

    int index = dvdfile.indexOf(dvdSku); 

    // Finds the dvd that matches sku entered, and prints out its attributes under correct rows. 
    String skuToFind = dvdfile.get(index); String titleToFind = dvdfile.get(index+1); String lengthToFind = dvdfile.get(index+2); 
    System.out.printf("%-10s %-15s %10s %n",skuToFind,titleToFind,lengthToFind); 
    kb.close(); 
} 

回答

0

即使扫描器不再有下一个元素来提供......抛出异常,它看起来就像在下一次调用。你需要检查Scanner#hasNext()调用Scanner#next()

之前的代码将

Scanner in = new Scanner(System.in); 
    boolean exit = false; 
    int choice = 0; 

    while (!exit) 
    { 
     System.out.println("1 Find an item.\n2 Display all items.\n3 Update item.\n4 Save item to disk.\n5 Quit."); 
     while(in.hasNext()){ 
     //next token available 
     if(in.hasNextInt()){ 
     /next token is an int value 
     choice = in.nextInt(); 
     } 
} 
} 

按照文档

public boolean hasNext() 

返回true,如果此扫描器的输入中有另一个标记。 此方法可能会阻止在等待输入以扫描。扫描仪不会超过任何输入。

+0

仍然得到与此相同的例外。它说行24,这将是在“choice = in.nextInt()” – Corey

+0

请现在看我的答案 – Keerthivasan

+0

旧的例外已消失。现在我正在获得一个新的。不知道为什么,它说它超出了界限,但是我测试的指数是索引+2是数组列表的最后一个元素,它甚至会打印出3个元素。
异常在线程 “主” java.lang.ArrayIndexOutOfBoundsException:-1 \t在java.util.ArrayList.elementData(未知来源) \t在java.util.ArrayList.get(未知来源) \t在version4.version4。 findItem(version4.java:69) \t at version4.version4.main(version4.java:31) – Corey