0

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();
}
dzimiks
  • 329
  • 1
  • 3
  • 14
MikeHazeJr
  • 11
  • 2
  • 3
    You probably call `nextInt()` before you call `nextLine();` The first method does not consume the new line character from the console so when the 2nd method executes it just grabs the new line alone. – takendarkk Jul 08 '18 at 21:42
  • Suggestion: use a loop and a string array rather than named variables – OneCricketeer Jul 08 '18 at 22:16

2 Answers2

0

Live demo: https://repl.it/repls/LimitedGlitteringComments

The reason why is .nextInt() just gets the next integer, not the newline or anything. So the next time you say .nextLine() it gets nothing but a \n. That's why after the .nextInt() I did input.nextLine();

Also I eliminated your big if, by putting in a for loop.

Hope this helped!

int numberOfPlayers;
Scanner input = new Scanner(System.in);
System.out.println("Enter number of players:");
numberOfPlayers = input.nextInt();
input.nextLine();
String[] arrayOfPlayers = new String[numberOfPlayers];
for (int i = 1; i <= numberOfPlayers; i++) {
  System.out.println("Enter name for Player " + i + ":");
  arrayOfPlayers[i-1] = input.nextLine();
}
// print list
for (String player : arrayOfPlayers) {
    System.out.println(player);
}

input.close();
Sheshank S.
  • 2,482
  • 2
  • 13
  • 33
  • I just don't have much experience yet, so bare with me. If I understand correctly, you are first solving my main issue with the 'input.nextLine()' after the 'nextInt()', then declaring a String array for the player names with a number of indexes based on the playerCount variable, populating it with names with the first for-loop, then displaying the names with another for-loop. I imagine I could create an int array for the scores for each player. How would I go about creating a struct? I want to have dart1,2,3/round. I figure I could do the main score with: for(round) > for(player) > for(dart#) – MikeHazeJr Jul 08 '18 at 22:16
  • Please make a new question for that, and I'll be happy to help you there :) If this answer helped remember to upvote and mark as the correct answer – Sheshank S. Jul 08 '18 at 22:19
0

Get your player count using the below code.

int playerCount = Integer.parseInt(input.nextLine());

nextInt() doesn't consume the newline character that you give after taking playerCount input. It gets consumed in the next nextLine() call.

Deepak Agrawal
  • 280
  • 1
  • 3
  • 14