0

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!

Mark Santos
  • 108
  • 10
  • No, if you test my code, you will notice that if you change from next() to nextLine() it still doesn't work as expected. – Mark Santos Nov 06 '19 at 21:33
  • Also https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo, you need to read the return line after the nextInt() – azro Nov 06 '19 at 21:35
  • That also did not help. :( – Mark Santos Nov 06 '19 at 21:37
  • @MarkSantos can you please post your actual output? – Shankha057 Nov 06 '19 at 21:38
  • 1
    I tried, it works, check with this code that you use the scnanner the same way for the 2 places I pointed in my comments https://ideone.com/1XzEHz – azro Nov 06 '19 at 21:40
  • That has actually worked for the first section of the problem, but if you input "yes", then we get the same issue happens again. I am going to update the problem so you can better understand what I am talking about. – Mark Santos Nov 06 '19 at 21:51

2 Answers2

0

Here is how I got what you may need:

    Set<String> list = new HashSet<>();
    Scanner scan = new Scanner (System.in);

    System.out.print("How many TV shows do you hope to watch this week? ");
    int i = Integer.parseInt(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): ");

    String showName;
    String answer;
    do {
        answer = scan.nextLine();
        if (answer.equalsIgnoreCase("no")) {
            break;
        } else {
            System.out.print("Which show? ");

            showName = scan.nextLine();
            if(list.contains(showName)){
                list.remove(showName);
            } else {
                System.out.println("That show is not on original list!");
            }

            System.out.println("Any other shows you're caught up with? (yes/no): "); // What yo do if I don't 
            // enter a "yes" or a "no"?
        }
    } while(answer.equalsIgnoreCase("yes"));

    System.out.println("Here's what you still have to watch this week:");
    System.out.println(list);
  1. First and foremost, I will advice you to use a Set<String> and not a List<String> as I did in my code.
  2. Secondly, think about the places where the user can enter values that are not supposed to be entered and how do you want to handle them. Like, when you want to enter the number of shows watched, if I enter a word, there will be an exception thrown, how to handle it? If I enter anything other than "yes" or "no", how to handle such a situation?
Shankha057
  • 1,072
  • 14
  • 30
0

I didn't see any problems where users would enter more than one string. Here is what I have:

          ArrayList<String> list = new ArrayList<String>();      
          Scanner scan = new Scanner (System.in);      
          int i; String in;

          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): ");
          in = scan.nextLine(); 

          if(in.equalsIgnoreCase("yes")) {
              boolean more = true;

              while(more) {
                  System.out.print("Which show? ");
                  String show = new String(scan.nextLine());
                  if(list.contains(show)){
                     list.remove(list.indexOf(show));
                  }else { 
                     System.out.println("That show is not on original list!");
                  }
                  System.out.print("Any other shows you're caught up with? (yes/no): ");
                  in = scan.nextLine(); 
                  if(!in.equalsIgnoreCase("yes")) {
                      more = false;
                  }
              }

          }

          System.out.println("Here's what you still have to watch this week:");
          System.out.println(list);

I used a while loop to detect whether the user had more shows that they were caught up with.

Brian
  • 21
  • 2