简体   繁体   English

按下Enter键时,Java扫描仪不接受输入

[英]Java Scanner not accepting input when enter pressed

The ultimate programming newbie here. 最终的编程新手。 Working through a Udemy course on Java. 完成有关Java的Udemy课程。 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. 在应对Udemy课程的挑战时,在这种特定情况下,当我在输入扫描仪信息后按Enter键时,它将继续进行下一行。 So I can press enter thousands of times and it just keeps going and going without ever progressing. 因此,我可以按Enter数以千计,并且它一直在不断前进,而从未取得进展。 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. 输入songName时,addSong方法会出现问题。 I've included the main method, incase the mistake I made is in there. 我已经包含了main方法,以防万一我犯了错误。 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. 编辑信息:main方法中的choice变量按预期工作。 User is expected to input an int for menu selection, and it works as expected. 用户应该输入一个int来进行菜单选择,并且它可以按预期工作。 The problem is with the String songName in the addSong method. 问题在于addSong方法中的字符串songName Entering any string (so for example "Song 1") and pressing enter, the cursor simply moves down a line in the IntelliJ console. 输入任何字符串(例如“ Song 1”),然后按Enter键,光标仅在IntelliJ控制台中向下移动一行。 This happens indefinitely. 这是无限期发生的。 Image attached of the IntelliJ console, showing the cursor several lines down after the input. IntelliJ控制台的图片附件,显示了在输入后几行光标。 Sorry if this isn't making much sense! 对不起,如果这没有什么意义! Ultimate newbie. 终极新手。

IntelliJ Console Cursor Position IntelliJ控制台光标位置

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, 我认为我发现问题不是需要监视ScannersearchAlbum()方法中的循环,因为由于无限循环而无法完成Scanner ,因此您不断按任何按钮,但由于循环而无法执行任何操作仍在运行
So you neet to fix the inner loop as j doesn't increment to reach tempSongList 因此,您无需修改​​内部循环,因为j不会递增以达到tempSongList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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