-1

私のメソッドは実行されますが、一度実行すると例外が発生します。理由がわかりません。例外に記載されている行 24 は、"choice=in.nextInt();" にあります。

ここに私の例外があります:

1 アイテムを探す
2 すべての項目を表示します。
3 アイテムを更新します。
4 項目をディスクに保存します。
5 終了します。
1
ファイルから項目を検索することを選択しました。
検索する DVD の SKU を入力します。
857295
857295 スターウォーズ 152
1 アイテムを探します。
2 すべての項目を表示します。
3 アイテムを更新します。
4 項目をディスクに保存します。
5 終了します。 java.util.Scanner.next(不明なソース) で java.util.Scanner.next(不明なソース)で java.util.Scanner.throwFor(不明なソース)
でスレッド「メイン」の java.util.NoSuchElementException 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(); 
} 
4

1 に答える 1