我的代码中的NoSuchElement

时间:2016-07-20 18:57:51

标签: java menu switch-statement nosuchelementexception

我遇到了这段代码的问题。它是不完整的,但我现在的目的是测试菜单是否有效,基本上我要做的是实现一个菜单。如果输入了无效字符,请继续提示进行有效选择。提示:在实现其他选项之前实施Quit。似乎无法弄清楚我做错了什么来得到这个错误。

import java.util.Scanner;

public class Playlist
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String title = "";

        System.out.println("Enter playlist's title: ");
        title = input.nextLine();        
        printMenu(title);
    }

    // Implement the printMenu() method. 
    // printMenu() takes the playlist title as a parameter and outputs a menu of options to manipulate the playlist. 
    // Each option is represented by a single character. 
    // Build and output the menu within the method.

    public static void printMenu(String playlistTitle)
    {
        Scanner input = new Scanner(System.in);
        boolean menu = true;
        String option;

        SongEntry songentry = new SongEntry();

        System.out.println(playlistTitle + " PLAYLIST MENU");
        System.out.println("a - Add song");
        System.out.println("d - Remove song");
        System.out.println("c - Change position of song");
        System.out.println("s - Output songs by specific artist");
        System.out.println("t - Output total time of playlist (in seconds)");
        System.out.println("o - Output full playlist");
        System.out.println("q - Quit");
        System.out.println("");        


        while(menu == true)
        {
            System.out.println("Choose an option: ");

                switch(option = input.next())
                {
                    case "q":

                        menu = false;

                        break;

                    case "o":

                        System.out.println(playlistTitle + " - OUTPUT FULL PLAYLIST");

                        break;

                    case "a":

                        System.out.println("ADD SONG");
                        System.out.println("Enter song's unique ID: ");
                        System.out.println("Enter song's name: ");
                        System.out.println("Enter artist's name: ");
                        System.out.println("Enter song's length: ");

                        break;

                    case "d":

                        System.out.println("REMOVE SONG");
                        System.out.println("Enter a song's unique ID: ");
                        System.out.println(" removed");

                        break;

                    case "c":

                        System.out.println("CHANGE POSITION OF SONG");
                        System.out.println("Enter a song's current position: ");
                        System.out.println("Enter a new position for song: ");
                        System.out.println(" moved to position ");

                        break;

                    case "s":

                        System.out.println("OUTPUT SONGS BY SPECIFIC ARTIST");
                        System.out.println("Enter artist's name: ");

                        break;

                    case "t":

                        System.out.println("OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)");
                        System.out.println("Total time: seconds");

                        break;
                }
            }
        }  
}

当我运行它时,我得到:

线程中的异常" main" java.util.Scanner.throwFor(Scanner.java:907)中的java.util.NoSuchElementException,位于Playlist.printMenu(Playlist.java:45)的java.util.Scanner.next(Scanner.java:1416) Playlist.main(Playlist.java:12)

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

在调用方法之前尝试关闭扫描仪,输入开关时打开两个扫描仪

答案 1 :(得分:0)

我实际上发现我需要将Scanner传递给printMenu方法。它现在有效。谢谢大家的建议!

import java.util.Scanner;

public class Playlist
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String title = "";

        System.out.println("Enter playlist's title: ");
        title = input.nextLine();        
        printMenu(title, input);
    }

    // Implement the printMenu() method. 
    // printMenu() takes the playlist title as a parameter and outputs a menu of options to manipulate the playlist. 
    // Each option is represented by a single character. 
    // Build and output the menu within the method.

    public static void printMenu(String playlistTitle, Scanner input)
    {

        boolean menu = true;
        String option;

        SongEntry songentry = new SongEntry();

        System.out.println(playlistTitle + " PLAYLIST MENU");
        System.out.println("a - Add song");
        System.out.println("d - Remove song");
        System.out.println("c - Change position of song");
        System.out.println("s - Output songs by specific artist");
        System.out.println("t - Output total time of playlist (in seconds)");
        System.out.println("o - Output full playlist");
        System.out.println("q - Quit");
        System.out.println("");        


        while(menu == true)
        {
            System.out.println("Choose an option: ");

                switch(option = input.next())
                {
                    case "q":

                        menu = false;

                        break;

                    case "o":

                        System.out.println(playlistTitle + " - OUTPUT FULL PLAYLIST");

                        break;

                    case "a":

                        System.out.println("ADD SONG");
                        System.out.println("Enter song's unique ID: ");
                        System.out.println("Enter song's name: ");
                        System.out.println("Enter artist's name: ");
                        System.out.println("Enter song's length: ");

                        break;

                    case "d":

                        System.out.println("REMOVE SONG");
                        System.out.println("Enter a song's unique ID: ");
                        System.out.println(" removed");

                        break;

                    case "c":

                        System.out.println("CHANGE POSITION OF SONG");
                        System.out.println("Enter a song's current position: ");
                        System.out.println("Enter a new position for song: ");
                        System.out.println(" moved to position ");

                        break;

                    case "s":

                        System.out.println("OUTPUT SONGS BY SPECIFIC ARTIST");
                        System.out.println("Enter artist's name: ");

                        break;

                    case "t":

                        System.out.println("OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)");
                        System.out.println("Total time: seconds");

                        break;
                }
            }
        }  
}