1

I have a method for adding an episode/multiple episodes, which uses sc.nextInt for validation, then sc.nextLine for the input of the name of the episode(s).

However, the sc.nextInt prevents the sc.nextLine from executing. I have tried parsing the integer from a string, but this leads to an input mismatch. Placing an sc.nextLine between the two also does not work. Any tips?

public void addepisodes(Scanner sc) {

int arraylistlength;

System.out.println("How many Episodes are in this Series?");

boolean valid=false;

    do{

    if(sc.hasNextInt()){

        valid=true;

        arraylistlength = Integer.parseInt(sc.nextLine());

        System.out.println("Please enter List of Episodes");
        for (int i = 0; i < arraylistlength; i++) {
            System.out.println("Episode " + (i + 1));
            String nextEp = sc.nextLine();
            list_of_episodes.add(nextEp);

                              }
                       }
    else{
        System.out.println("Error, input an integer");
        sc.next();
        }
    }while(!valid);
                 } 
YCF_L
  • 49,027
  • 13
  • 75
  • 115

0 Answers0