In this while loop, it's asking for names of TV shows and the duration of the shows. When the user inputs an empty string for the show name, the loop is supposed to break. This works for the first cycle but as soon as it asks for the name the second time, the program breaks. I think it is skipping over the
String showTitle = scanner.nextLine();
and going straight to the if statement. Could someone please help me understand what is happening? Thank you.
public static void main(String[] args) {
// implement here your program that uses the TelevisionProgram class
ArrayList<TelevisionProgram> programs = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Name: ");
String showTitle = scanner.nextLine();
if (showTitle.isEmpty()) {
break;
}
System.out.print("Duration: ");
int minutes = scanner.nextInt();
programs.add(new TelevisionProgram(showTitle, minutes));
}
}
The other class looks like this, but I don't think it's relevant to solve the problem I'm having. Here it is, just in case...
public class TelevisionProgram {
private String name;
private int duration;
public TelevisionProgram(String name, int duration) {
this.name = name;
this.duration = duration;
}
public boolean isAwesome() {
return this.name.contains("MasterChef");
}
public String getName() {
return this.name;
}
public int getDuration() {
return this.duration;
}
@Override
public String toString() {
return this.name + ", " + this.duration + " minutes";
}
}
Aucun commentaire:
Enregistrer un commentaire