Я создаю список массивов со строками, которые пользователь вводит в консоли. Проблема в том, что когда они вводят строку, содержащую более 2 слов, программа работает не так, как ожидалось.
Это то, что у меня есть:
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);
Кстати, Я попытался перейти от next () к nextLine (), он по-прежнему не работает должным образом.
Вот мои ожидаемые результаты:
Пример 1: Предполагается, что вы не поймалидо любых шоу (Это работает, благодаря ответу пользователя, внизу)
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]
Пример 2: Предполагается, что вы подключились к одному шоу и обновите список (Не работает)
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]
Пример 3: Предполагается, что вы перехватили одно шоу, но его нет в списке. (Он не обновляет список, так как не имеет смысла добавлять новое шоу, в которое вы попали, просто для его удаления.)
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]
Спасибо!