简体   繁体   中英

Java Scanner not accepting input when enter pressed

The ultimate programming newbie here. Working through a Udemy course on Java. Getting a bit confused with the details of the scanner.

Working on a challenge from the Udemy course, and in this specific case, when I press enter after inputting the information for the scanner, it just goes to the next line and keeps doing that. So I can press enter thousands of times and it just keeps going and going without ever progressing. The same code in another project seems to work, but this one specific project won't.

The problem occurs with the addSong method when entering songName. I've included the main method, incase the mistake I made is in there. The scanner inputs in the main method seem to work fine. Thanks for any help!

Edit for info: The choice variable in the main method works as expected. User is expected to input an int for menu selection, and it works as expected. The problem is with the String songName in the addSong method. Entering any string (so for example "Song 1") and pressing enter, the cursor simply moves down a line in the IntelliJ console. This happens indefinitely. Image attached of the IntelliJ console, showing the cursor several lines down after the input. Sorry if this isn't making much sense! Ultimate newbie.

IntelliJ Console Cursor Position

public class Main {

   private static Scanner scanner = new Scanner(System.in);

   public static LinkedList addSong(LinkedList<Song> Playlist, ArrayList<Album> AlbumList){

    String songName;
    System.out.println("Enter song name: ");
    songName = scanner.nextLine();

    Song newSong = searchAlbums(songName,AlbumList);
    System.out.println("Accepted");
    if(newSong==null){
        System.out.println("Song does not exist.");
    } else {
        Playlist.add(newSong);
        System.out.println("Song added.");
    }



    return Playlist;
}

public static void main(String[] args) {
    ArrayList<Album> AlbumList = createAlbumList();
    LinkedList<Song> Playlist = createInitialPlaylist(AlbumList);

    boolean quit = false;
    int choice;

    while(!quit){
        System.out.println("================");
        System.out.println("Menu");
        System.out.println("1. Add Song to Playlist");
        System.out.println("2. View Playlist");
        System.out.println("3. View Current Song");
        System.out.println("4. Replay Current Song");
        System.out.println("5. Next Song");
        System.out.println("6. Previous Song");
        System.out.println("7. Remove Song from Playlist");
        System.out.println("8. Quit");
        choice = scanner.nextInt();
        scanner.nextLine();


        switch(choice){
            case 1:
                addSong(Playlist,AlbumList);
                break;
            case 2:
                viewPlaylist(Playlist);
                break;
            case 3:

                break;
            case 4:

                break;
            case 5:

                break;
            case 6:

                break;
            case 7:

                break;
            case 8:
                quit = true;
                break;
            default:
                System.out.println("Invalid choice. Choose 1-8");
                break;
        }
    }
}

public static Song searchAlbums(String songName, ArrayList<Album> AlbumList){
    Album tempAlbum;
    ArrayList<Song> tempSongList;
    Song tempSong;
    for(int i=0; i<AlbumList.size(); i++){
        tempAlbum = AlbumList.get(i);
        tempSongList = tempAlbum.getSongList();
        for(int j=0; j<tempSongList.size(); i++){
            tempSong = tempSongList.get(j);
            if(tempSong.getSongTitle().equals(songName)){
                return tempSong;
            }
        }
    }

    return null;
}

I think I found the problem it's not the Scanner who is need to be watched it's the loop in searchAlbum() method as it doesn't finish due to infinite loop so you keep pressing any thing but it doesn't do anything because the loop is still running,
So you neet to fix the inner loop as j doesn't increment to reach tempSongList

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM