扫描程序在方法之后运行到NoSuchElementException。这是为什么?

时间:2013-02-24 18:55:58

标签: java java.util.scanner

Exception in thread "main" java.util.NoSuchElementException 
     at java.util.Scanner.throwFor(Unknown Source) 
     at java.util.Scanner.next(Unknown Source) 
     at java.util.Scanner.nextInt(Unknown Source) 
     at java.util.Scanner.nextInt(Unknown Source) 
     at MainMenu.main(MainMenu.java:27)

使用我的类在文件中创建行后,我收到此错误。这是错误的位置。

        I was told to post the initialization.
        Scanner mainmenu = new Scanner(System.in);  


        System.out.println("Ramen Cookbook");
        System.out.println("");
        System.out.print("Hello, select your option by typing a number below \n");
        System.out.println("0. Print List");
        System.out.println("1. Create New Recipe");
        System.out.println("2. Sort Cookbook");
        System.out.println("3. Exit Program\n");

        System.out.println("Please enter your choice...");

        int choice = mainmenu.nextInt();

这是选择变量后面的代码。

        choice = Integer.parseInt(selection.nextLine());


                switch (choice) 

                {
                    case 0: //Print option
                        System.out.println("Print Option chosen\n");
                        control.Reader();
                        break;
                    case 1: //Create option
                        System.out.println("You've chosen to create a recipe\n");
                        RecipeReader.AddReci();
                        break;

                    case 2: //Sort option
                        System.out.println("You've chosen to sort your recipe book\n");

                        System.out.println("Would you like to use acending or decending order? ");
                        System.out.println("0.Acending\n1.Decending\n");
                        int orderchoice = mainmenu.nextInt();
                 RecipeReader.sort(orderchoice);

                        System.out.println("sorted");
                        break;
                    case 3: //Exit option
                        System.out.println("Are you sure you want to exit?");
                        System.out.println("(y or n)");
                        String yon = mainmenu.next(); 


                        if (yon.equalsIgnoreCase("y")) //Error checking will accept "Y" or         "y"
                        {
                            quit=true;
                            System.out.println("\nGoodbye...");
                            break;
                        }
                        else
                        {
                            break;
                        }
                     default: //error checking. Ensuring the user picks a number 1-5
                         System.out.println("Please type in a number 1-5.\n1");

然而,我不认为这是导致问题的原因,因为如果我选择任何其他选项,主菜单就像发条一样。以下是在文件中创建配方的方法。

public static void AddReci() throws IOException
{
    String recipebook[] = new String[1000];
    int maxIndex = -1;
    Scanner scanner = new Scanner(new File("recipes.txt"));

    while(scanner.hasNext())
     {
      maxIndex++;
      recipebook[maxIndex] = scanner.nextLine();
     }

    scanner.close();

    String recipe, ing1, ing2, ing3;
    Scanner input = new Scanner(System.in);

  PrintWriter out = new PrintWriter(new FileWriter("recipes.txt", true));

    System.out.println("Recipe Name? ");
    recipe = input.nextLine();
    out.println(recipe);

    System.out.println("1st Ingredient? ");
    ing1 = input.nextLine();
    out.println(ing1);

    System.out.println("2nd Ingredient? ");
    ing2 = input.nextLine(); 
    out.println(ing2);

    System.out.println("3rd Ingredient? ");
    ing3 = input.nextLine(); 
    out.print(ing3);
    System.out.println(recipe + " is added to the database.\n");
    out.close();             
    }

0 个答案:

没有答案