1

Selecting each option on the menu now works fine (thank you for the help Tarek Salah and dasblinkenlight). The problem now being that when I select an option that requires me to enter a new word (for example option 3 the user must enter the name of a song) it skips over that and goes back to the menu. Does anyone know how to stop that from happening so that the user can actually enter something?

import java.util.Scanner;
public class JukeboxApp { 
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Jukebox jb = new Jukebox();
    boolean check = false;
    System.out.println("Please enter the corresponding number to perform said action.");
    while ( check == false ) {
        System.out.println("1: Add a song to the JukeBox\n" +
                            "2: Remove a song from the JukeBox\n" +
                            "3: Search for a specific song\n" +
                            "4: Display total price to play all songs\n" +
                            "5: Display the most expensive song\n" +
                            "6: Display the shortest song\n" +
                            "7: Display the most played song\n" +
                            "8: Display all songs in the JukeBox\n" +
                            "9: Display all songs by a specific artist\n" +
                            "10:\n" +
                            "11: Exit the JukeBox");
        int num = sc.nextInt();
        if ( num == 1 ) {
            System.out.println("Please enter the name of the artist");
            String artist = sc.next();
            sc.nextLine();
            System.out.println("Please enter the title of the song");
            String title = sc.nextLine();
            System.out.println("Please enter the price of the song");
            double price = sc.nextDouble();
            System.out.println("Please enter the length of the song");
            double length = sc.nextDouble();
            Song s1 = new Song(artist, title, price, length);
            jb.addSong(s1);
        } else if ( num == 2 ) {
            System.out.println("Please enter the title of the song you would like to remove");
            sc.nextLine();
            jb.removeSong(sc.nextLine());
        } else if ( num == 3 ) {
            System.out.println("Enter the title of the song you are searching for");
            jb.searchSong(sc.nextLine());
        } else if ( num == 4 ) {
            System.out.println(jb.calcTotal());
        } else if ( num == 5 ) {
            System.out.println(jb.showMostExpensive());
        } else if ( num == 6 ) {
            System.out.println(jb.showShortest());
        } else if ( num == 7 ) {
            System.out.println(jb.mostPlayed());
        } else if ( num == 8 ) {
            jb.displaySongs();
        } else if ( num == 9 ) {
            System.out.println("Please enter the artist you are searching for");
            System.out.println(jb.searchArtist(sc.nextLine()));
        } else if ( num == 10 ) {
            System.out.println("");
        } else if ( num == 11 ) {
            System.out.println("Thank you for using my JukeBox.");
            check = true;
        }
    }
}

}
1
  • use if {} elseif{} ... Commented Dec 19, 2013 at 21:13

2 Answers 2

2

The reason this does not work is that you may be calling sc.nextInt() multiple times, when you expect your user to enter only one value.

You should store the result in a variable, and use that variable in all your if statements. Alternatively, you could use a switch statement.

var userEntry = sc.nextInt();
sc.nextLine(); // skip to the end of the line
if ( userEntry  == 1 ) {
    ...
} else if ( userEntry  == 2 ) {
    ...
} else if ( userEntry  == 3 ) {
    ...
} else ...

or

var userEntry = sc.nextInt();
sc.nextLine(); // skip to the end of the line
switch ( userEntry ) {
    case 1:
        ...
    break;
    case 2:
        ...
    break;
    case 3:
        ...
    break;
    default:
        ...
    break;
}
Sign up to request clarification or add additional context in comments.

2 Comments

That works for selecting what I want to do but now when it runs one of the if functions that require me to enter a new word, it won't let me and just exits the if and loops back around. For example, option 3: The user should be able to search for a song but it skips over that and goes back to the menu.
@Michael_D That's because nextLine() gets the end-of-line that you enter right after the menu digit. You need to call nextLine() after nextInt() (see my edit).
0

Change if to else if and take input nextInt() for one time in each loop

int num = sc.nextInt()
if (num  == 1 ) {

}
else if ( num == 2 ) {

}
else if ( num == 3 ) {

}
 ....

1 Comment

That works for selecting what I want to do but now when it runs one of the if functions that require me to enter a new word, it won't let me and just exits the if and loops back around. For example, option 3: The user should be able to search for a song but it skips over that and goes back to the menu.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.