0

I'm having problems with this code. It is incomplete, but my purpose right now is to test if the menu works, Basically what I am trying to do is implement a menu. If an invalid character is entered, continue to prompt for valid choice. Hint: Implement Quit before implementing other options. Cant seem to figure out what I am doing wrong to get this error.

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;
                }
            }
        }  
}

When I run it, I get:

Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1416) at Playlist.printMenu(Playlist.java:45) at Playlist.main(Playlist.java:12)

Thanks for the help in advance.

2 Answers2

0

Try closing the scanner before you call the method, you have two scanners open when you enter the switch

Ethan Williams
  • 78
  • 1
  • 15
  • Actually, closing the scanner would also close the underlying stream (`System.in`), rendering the other scanner unusable. Best to only use one scanner object and pass it around if needed. However, I think the actual problem has to do with not checking the state of the scanner before invoking `next()`. – Mick Mnemonic Jul 20 '16 at 19:42
0

I actually found that I needed to pass the Scanner into the printMenu method. It works now. Thanks everyone for the suggestions!

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;
                }
            }
        }  
}