I am writing a code that asks for user input on the names of the players and stores them in array. To do this I am using a for loop, which i thought would be more efficient, however, the code seems to do something unexpected, and I cannot see why it is doing what it does. Below is the code and the result:
Scanner reader = new Scanner (System.in);
System.out.print("Please enter the number of players:");
int players = reader.nextInt();
String[] player_name = new String[players+1];//Array to store player names
for (int i = 0; i < players; i++)//Loop to ask players their name
{
System.out.print("Player " + (i+1) + " please enter your name:\n");//Asks player names one by one
player_name[i] = reader.nextLine();//Saves the player names to the array
}
And here is the result when the number of players is 2:
Please enter the number of players:2
Player 1 please enter your name:
Player 2 please enter your name:
wa
BUILD SUCCESSFUL (total time: 23 seconds)
When I hit enter after typing the "name" wa to type player 2's name the program just ends.