我遇到了抛出 NoSuchElementException 的问题。它被抛出,因为流已关闭,但我从未关闭流。问题发生在int selection = in.nextInt()
的代码的早期。我一直试图纠正这个问题几个小时没有运气。任何帮助将非常感激。谢谢!
import java.util.Scanner;
public class LoadMenu {
private Menu main = new Menu();
private EditMenu editSong = null;
public LoadMenu(Menu menu) {
main = menu;
}
private LoadMenu() {
}
public PlayList loadListMenu(PlayList list) throws Exception {
Scanner in = new Scanner(System.in);
int remove, edit;
System.out.println("What would you like to do?");
System.out.println();
System.out.println("(1) Add Songs");
System.out.println("(2) Remove Song");
System.out.println("(3) Edit Song");
System.out.println("(4) View Playlist");
System.out.println("(5) Clear Playlist");
System.out.println("(6) Save Playlist");
System.out.println("(7) Main Menu");
System.out.println("(8) Exit");
System.out.println();
System.out.print("Please make a selection: ");
System.out.println(System.in.available());
int selection = in.nextInt();
System.out.println();
if (selection == 1) {
int num;
System.out.print("How many songs would you like to add to " + list.getName() + ": ");
num = in.nextInt();
System.out.println();
if (num > 1) {
for (int i = 0; i < num; i++) {
Function newSong = new Function(list);
System.out.println("Enter information for song " + (i + 1) + ":");
Song addSong = newSong.newSong();
list.addSong(addSong);
System.out.println();
}
this.loadListMenu(list);
} else {
Function newSong = new Function(list);
Song addSong = newSong.newSong();
list.addSong(addSong);
this.loadListMenu(list);
}
} else if (selection == 2) {
System.out.println("Which song would you like to remove?");
for (int i = 0; i < list.numSongs(); i++) {
System.out.println(
"(" + (i + 1) + ") " + list.getSong(i).getTitle() + " by " + list.getSong(i).getArtist());
}
System.out.println();
System.out.print("Song number to remove: ");
remove = in.nextInt();
list.removeSong(remove - 1);
this.loadListMenu(list);
} else if (selection == 3) {
System.out.print("Which song would you like to edit?");
System.out.println();
if (list.numSongs() == 0) {
System.out.println("There are no songs saved in " + list.getName());
} else {
for (int i = 0; i < list.numSongs(); i++) {
System.out.println(
"(" + (i + 1) + ") " + list.getSong(i).getTitle() + " by " + list.getSong(i).getArtist());
}
System.out.println();
System.out.print("Make a selection: ");
edit = in.nextInt();
System.out.println();
editSong = new EditMenu(list, edit);
list = editSong.editSong();
}
this.loadListMenu(list);
} else if (selection == 4) {
if (list.numSongs() == 0) {
System.out.println("There are no songs saved in " + list.getName());
}
for (int i = 0; i < list.numSongs(); i++) {
System.out.println(
"(" + (i + 1) + ") " + list.getSong(i).getTitle() + " by " + list.getSong(i).getArtist());
}
System.out.println();
this.loadListMenu(list);
} else if (selection == 5) {
list.clearList();
this.loadListMenu(list);
} else if (selection == 6) {
PlayListWrite saveFile = new PlayListWrite(list);
saveFile.saveFile();
this.loadListMenu(list);
} else if (selection == 7) {
main.mainMenu(list);
} else if (selection == 8) {
System.exit(0);
}
return list;
}
}