1

In my code, the ArrayList will print to the console. From there the user will input a name from the list. It will check the name to the list. I have my code checking user input 3 separate times. The issue I'm having is that sometimes it goes thru the code correctly until you enter an incorrect value. Then it just goes to the end where it says "I'm outside everything". I only use that to see where my code is. I'm wondering where my code is going wrong. How can I fix this? Any assistance is greatly appreciated!

        ArrayList<String> forwards = new ArrayList<String>();
    
        forwards.add("Matthew Barzal");
        forwards.add("Josh Bailey");
        forwards.add("Anthony Beauvillier");
        forwards.add("Kieffer Bellows");
        forwards.add("Casey Cizikas");
        forwards.add("Cal Clutterbuck");
        forwards.add("Anders Lee");
        forwards.add("Matt Martin");
        forwards.add("Brock Nelson");
        forwards.add("Oliver Wahlstrom");
        forwards.add("Zach Parise");
        forwards.add("Kyle Palmieri");
        forwards.add("JG Pageau");

    
    System.out.println("These are your starting forwards:");
    for (int i = 0; i < forwards.size(); i++) {
        System.out.println(forwards.get(i));
        }
    System.out.println();
    
    System.out.println("Please pick three(3) forwards to be line in #1:");
    Scanner input = new Scanner(System.in);
    String userInput;
    String userInput2 = "";
    String userInput3 = "";

userInput = input.nextLine();
    
    
    while (true) {
        
        for(String i : forwards) {
            if(userInput.equals(i)) {
                System.out.println("Please pick another forward...");
                System.out.println("I am in first if statement");
                userInput2 = input.nextLine();
                continue;
               // return;
                } else if (userInput2.equals(i)) {
                    System.out.println(userInput);
                    System.out.println("Please pick another forward...");
                    System.out.println("I am in second if statement");
                    userInput3 = input.nextLine();
                    //continue;
                
                } else if (userInput3.equals(i)) {
                
                        System.out.println("I am in third if statement");
                        System.out.println("Your forwards for Line # 1 are: " + 
                        userInput + " " + userInput2 + " " + userInput3);
                        break;
                        //return;
                        
                    } 
        
        }
    
        System.out.println("I am outside everything");
        System.out.print("Input not found in forwards list. Enter new value: \n");
        userInput = input.nextLine();
        continue;
    }
5
  • I see you are a New York Islanders fan :) Can you clarify what you mean by incorrect value? Are you saying the code jumps to the end if you enter a name not in forwards? Commented Oct 30, 2021 at 4:53
  • Yes I am a huge Islanders fan!!! So basically, the user is inputing a name from ArrayList forwards using the Scanner. If the name doesn't match then it should jump to the end giving the "Input not found error". But it should go back to the beginning to have the user enter another name. Sometimes you enter the correct name so it goes to the 2nd if statement but entering the second name correctly it jumps back down to "Input not found error". Even thought the name is correct. idk Commented Oct 30, 2021 at 5:01
  • I'm able to reproduce the issue you're describing by entering a 2nd name which is earlier in the list than the 1st one. For example, if you choose Zach Parise then Anders Lee, you get that problem, but the other way around is fine. The issue is your loop has advanced to the first player, so it will not be able to find any subsequent player which comes earlier in the list. Commented Oct 30, 2021 at 5:15
  • Tyler, my brother, you hit the jackpot. This is exactly how I wanted the application to work. Thank you so much!! So are you in NY and are you an Islanders fan also? Commented Oct 30, 2021 at 13:46
  • And,I pretty much understand what you did by looking at the code. You basically added every entry from the user into a new List called selectedForwards and deleted that entry from the original Set list called forwards. By, using the following condition (selectedForwards.size() < 3) you limited the entries to 3. Awesome! Again, thank you for the assistance. Commented Oct 30, 2021 at 13:56

1 Answer 1

2

I've refactored your code to be shorter and simpler, using a set. Take a look at this and see if it meets your needs. It should be working correctly now.

        Set<String> forwards = new HashSet<>();
    
        forwards.add("Matthew Barzal");
        forwards.add("Josh Bailey");
        forwards.add("Anthony Beauvillier");
        forwards.add("Kieffer Bellows");
        forwards.add("Casey Cizikas");
        forwards.add("Cal Clutterbuck");
        forwards.add("Anders Lee");
        forwards.add("Matt Martin");
        forwards.add("Brock Nelson");
        forwards.add("Oliver Wahlstrom");
        forwards.add("Zach Parise");
        forwards.add("Kyle Palmieri");
        forwards.add("JG Pageau");

    
        System.out.println("These are your starting forwards:");
        for (String forward : forwards) {
            System.out.println(forward);
        }
        
        System.out.println("Please pick three(3) forwards to be line in #1:");
        Scanner input = new Scanner(System.in);
        List<String> selectedForwards = new ArrayList<>();
        String userInput;
    
        while (selectedForwards.size() < 3) {
            System.out.println("Select the next forward: \n");
            userInput = input.nextLine();
            if (!forwards.contains(userInput)) {
                System.out.println("Input not found in forwards list. Enter new value: \n");
                continue;
            }
            selectedForwards.add(userInput);
            forwards.remove(userInput);
        }
        
        System.out.println(selectedForwards);
    
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.