I am creating an Array List with Strings that the user inputs in the console. The problem is that when they input a String with more than 2 words, the program doesn't work as expected.
This is what I have so far:
ArrayList<String> list = new ArrayList<String>();
Scanner scan = new Scanner (System.in);
int i;
System.out.print("How many TV shows do you hope to watch this week? ");
i = scan.nextInt();
scan.nextLine();
for(int j = 0; j<i; j++){
System.out.print("Enter show " + (j+1) + ": ");
list.add(scan.nextLine());
}
System.out.print("Have you caught up to any shows (answer yes or no): ");
while (scan.nextLine().equalsIgnoreCase("yes")){
System.out.print("Which show? ");
String show = new String(scan.nextLine());
if(list.contains(show)){
list.remove(list.indexOf(show));
}else {
System.out.print("That show is not on original list!");
}
}
System.out.println("Here's what you still have to watch this week:");
System.out.println(list);
By the way, I have tried to change from next() to nextLine() it still doesn't work as expected.
My expected outputs are these:
Sample 1: Assumes you haven’t caught up to any shows (This works, thanks to the response from a user, down below)
How many TV shows do you hope to watch this week? 3
Enter show 1: RWBY
Enter show 2: Kengan Ashura
Enter show 3: The Good Place
Have you caught up to any shows (answer yes or no): no
Here's what you still have to watch this week:
[RWBY, Kengan Ashura, The Good Place]
Sample 2: Assumes you’ve caught up to one show and will update the list (Not working)
How many TV shows do you hope to watch this week? 3
Enter show 1: Father Brown
Enter show 2: Death in Paradise
Enter show 3: Watchmen
Have you caught up to any shows (answer yes or no): yes
Which show? Death in Paradise
Any other shows you're caught up with? (yes/no) no
Here's what you still have to watch this week:
[Father Brown, Watchmen]
Sample 3: Assumes you’ve caught up to one show but it’s not on the list. (It does not update the list, as it doesn’t make sense to add a new show you’re caught up with to the list, just to remove it.)
How many TV shows do you hope to watch this week? 2
Enter show 1: Watchmen
Enter show 2: RWBY
Have you caught up to any shows (answer yes or no): yes
Which show? Monday Night Football
That show is not on original list!
Any other shows you're caught up with? (yes/no) no
Here's what you still have to watch this week:
[Watchmen, RWBY]
Thank you!