[Solved] Java not continuing a loop [closed]

[ad_1]

public static void main(String[] args)
    {
        Scanner numPlayers = new Scanner(System.in);
        ArrayList<Player> playerList = new ArrayList<>();
        int input = numPlayers.nextInt();

        for (int i = 0; i < input; i++){
            System.out.println("what is player " + (i + 1) + " name?");
            String playerName = numPlayers.next();
            playerList.add(new Player(playerName));
        }

    }

You should have declared scanner object outside the for the loop. The problem with your code was each time after taking a string input your code was needed to provide an integer for (int i = 0; i < (numPlayers.nextInt()-1); i++) and that’s why if you provide anything but an integer it gives InputMismatchException. So you have to initialize the input constant outside the for loop otherwise execution will vary dynamically.

[ad_2]

solved Java not continuing a loop [closed]