Home > Software engineering >  How to check user input from an ArrayList multiple times in java
How to check user input from an ArrayList multiple times in java

Time:10-31

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;
    }

CodePudding user response:

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);
    
        }
  • Related