-2

I'm doing the Java MOOC fi course. I'm supposed to make a list of show names and durations, and print the ones that are shorten than certain duration.
I want the while loop to end after I enter a blank name but it ends after the first object I add to the list.

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    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 programName = scanner.nextLine();
            if (programName.isEmpty()) {
                break;
            }
            System.out.print("Duration: ");
            int durationOfProgram = scanner.nextInt();
            programs.add(new TelevisionProgram(programName, durationOfProgram));
        }
        System.out.println();
        System.out.print("Program's maximum duration? ");
        int maxDuration = scanner.nextInt();
        for (TelevisionProgram show : programs) {
            if (show.getDuration() <= maxDuration) {
                System.out.println(show);
            }
        }

    }
}
Dada
  • 6,313
  • 7
  • 24
  • 43
Alex Roman
  • 61
  • 4

1 Answers1

0

replaced

int durationOfProgram = scanner.nextInt();

with

int durationOfProgram = Integer.valueOf(scanner.nextLine());

and now it works but i still don't get why

Alex Roman
  • 61
  • 4