我创建了一个Song类,其中包含歌曲(标题,艺术家,专辑)的数据成员。我已经有一个.txt文件,其中包含存储在数组列表中的不同歌曲。在我的主要课程中,其中一项功能是允许用户通过标题,艺术家或专辑搜索歌曲。
我的问题是当用户输入某些标题时搜索功能找不到该歌曲。例如,当我搜索歌曲标题" Stay"它找到了。但是,当我搜索名为" Bohemian Rhapsody"它无法找到它存储。我知道它是存储的,因为当我显示它显示的列表时。按专辑或艺术家搜索时也会出现此问题。
我认为我的问题是由于我分割字符串的方式或者它只是搜索功能本身的问题而发生的。
这是我的Song类,包含Constructor和Get / Set Methods
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;
}
}
这是我的主类,它从.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;
}
}
答案 0 :(得分:0)
将String searchArtist = input.next();
更改为String searchArtist = input.nextLine();
使用.next()
您无法输入超过1个字。收到空格后,它将停止捕获更多输入。
我建议您将所有3种输入扫描方式更改为.nextLine()
。