从文本文件中搜索和显示 ArrayList 的内容

Search and Display Contents of ArrayList from Text File

我创建了一首歌曲 class,其中包含歌曲的数据成员(标题、艺术家、专辑)。我已经有一个 .txt 文件,其中包含存储在数组列表中的不同歌曲。在我的主要 class 中,其中一个功能是允许用户按标题、艺术家或专辑搜索歌曲。

我的问题是,当用户输入某些标题时,搜索功能找不到歌曲。例如,当我搜索歌曲名称 "Stay" 时,它找到了。但是,当我搜索标题为 "Bohemian Rhapsody" 的歌曲时,找不到已存储的歌曲。我知道它已存储,因为当我显示列表时它会显示出来。按专辑或艺术家搜索时也会出现此问题。

我认为我的问题是由于我拆分字符串的方式造成的,或者可能只是搜索功能本身的问题。

这是我的歌曲 class,带有构造函数和 Get/Set 方法

public class Song {
    //Declaring all data members.
    private String title;
    private String artist;
    private String album;
    private String length;
    private static int songCounter = 0;

    //Constructors for Song class.      
    public Song(String title, String artist, String album, String length){
        this.title = title;
        this.artist = artist;
        this.album = album;
        this.length = length;
        songCounter++;
    }
    //Get and Set methods 
    public String getTitle(){
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist(){
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getAlbum(){
        return album;
    }

    public void setAlbum(String album){
        this.album = album;
    }

    public String getLength(){
        return length;
    }

    public void setLength(String length){
        this.length = length;
    }

    public static int getSongCounter(){
        return songCounter;
    }
    //Overriding the toString() function.
    public String toString(){
        return title +" "+artist+" "+album+" "+length;
    }
 }

这是我的 Main class,它从 .txt 文件中读取并包含搜索方法

public class Library {

public static void main(String[] args) {
    ArrayList <Song> songList = new ArrayList <Song> ();
    boolean testInput = true;

    try{
        Scanner read = new Scanner (new File("SongList.txt"));
        do{
            String line = read.nextLine();
            String [] tokens = line.split(",");
            songList.add(new Song(tokens[0], tokens[1], tokens[2], tokens[3]));
        }while(read.hasNext());
        read.close();
    }catch (FileNotFoundException e){
        System.out.println("File not found.");
    }

    while ( testInput ){
        System.out.println("\nSelect a Function");
        System.out.println("1. Search Song");
        System.out.println("2. Add Song");
        System.out.println("3. Delete Song");
        System.out.println("4. Display Songs");
        System.out.println("5. Quit");

        switch (MenuInputCheck(1, 5)){

        case 1: searchSong(songList);
                break;
        case 2: addSong(songList);
                break;
        case 3: deleteSong(songList);
                break;
        case 4: displaySong(songList);
                break;
        case 5: testInput = false;
                break;
        }
    }
}

public static void searchSong(ArrayList <Song> songList){
    Scanner input = new Scanner(System.in);
    System.out.println("A. Search by Title");
    System.out.println("B. Search by Artist");
    System.out.println("C. Search by Album");

    boolean found = false;
    char menuOption = input.next().charAt(0);

    switch (menuOption) {
    case 'A':
    case 'a': System.out.print("Enter song title: ");
              String searchTitle = input.nextLine();
              for (Song i : songList){
                  if (i.getTitle().equals(searchTitle)){
                      System.out.println(i);
                      found = true;
                  }
              }
              if ( found != true ){
                  System.out.println("Song does not exist.");
              }
              break;
    case 'B':
    case 'b': System.out.print("Enter song artist: ");
              String searchArtist = input.nextLine();
              for (Song i : songList){
                  if (i.getArtist().equals(searchArtist)){
                      System.out.println(i);
                      found = true;
                  }
              }
              if ( found != true ){
                  System.out.println("Song does not exist.");
              }
              break;
    case 'C':
    case 'c': System.out.print("Enter song album: ");
              String searchAlbum = input.nextLine();
              for (Song i : songList){
                  if (i.getAlbum().equals(searchAlbum)){
                      System.out.println(i);
                      found = true;
                  }
              }
              if ( found != true ){
                  System.out.println("Song does not exist.");
              }
              break;
    }
}

String searchArtist = input.next();更改为String searchArtist = input.nextLine();

使用 .next() 您无法输入超过 1 个单词。它将在收到 space.

后停止捕获进一步的输入

我建议您将所有 3 个输入扫描方法更改为 .nextLine()