I have started work on a program to keep track of scores for my buddies and I when we play darts. Unfortunately when I enter the number of players and the method asks for player names, it skips over the input for player 1. I used input.nextLine() since I need to be able to record first and last names in one string at times. It works just fine for each subsequent player, but bypasses player 1 by default. The code is as follows:
if (playerCount >= 1)
{
System.out.println("Enter name for Player 1: ");
name1 = input.nextLine();
}
if (playerCount >= 2)
{
System.out.println("Enter name for Player 2: ");
name2 = input.nextLine();
}
if (playerCount >= 3)
{
System.out.println("Enter name for Player 3: ");
name3 = input.nextLine();
}
if (playerCount >= 4)
{
System.out.println("Enter name for Player 4: ");
name4 = input.nextLine();
}
if (playerCount >= 5)
{
System.out.println("Enter name for Player 5: ");
name5 = input.nextLine();
}
if (playerCount >= 6)
{
System.out.println("Enter name for Player 6: ");
name6 = input.nextLine();
}
if (playerCount >= 7)
{
System.out.println("Enter name for Player 7: ");
name7 = input.nextLine();
}
if (playerCount >= 8)
{
System.out.println("Enter name for Player 8: ");
name8 = input.nextLine();
}
Once I type, for example, 4 players, I get this:
Enter number of players: 4 Enter name for Player 1:
Enter name for Player 2: Johnny Bravo
Enter name for Player 3: Kevin Bacon
Enter name for Player 4: Kyle
Players: 4
[this space is blank]
Johnny Bravo
Kevin Bacon
Kyle
Please let me know why it is doing this, as well as why replacing the player1 if contents with the following works, despite the code redundancy:
if (playerCount >= 1)
{
System.out.println("Enter name for Player 1: ");
name1 = input.nextLine();
name1 = input.nextLine();
}