0

I am writing a random chance game to pick a random winner. I am using a for loop to input the players into an array, but it doesn't let me input anything for the first player. Here is the code:

import java.util.Scanner;
import java.util.Random;
public class Run {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    Random rand = new Random();

    System.out.println("How many people will play???");
    int playernum = input.nextInt();

    String players[] = new String[playernum];

    for(int i = 0; i < playernum; i++){
        System.out.println("Who is player #" + (i+1)+"?");
        players[i] = input.nextLine();
    }

    System.out.println("The winner is: " + players[rand.nextInt(playernum)]);

}

}
HAL 9000
  • 61
  • 1
  • 6
  • 1
    THINK! You create an `array` of string based on `playernum` as it's size and then try to loop through something that increases... – Tdorno Jul 02 '13 at 03:04
  • 2
    possible duplicate of [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx) – acdcjunior Jul 02 '13 at 03:08

4 Answers4

4

The input.nextInt() call reads the integer but leaves the new line character unread in the input stream, so the input.nextLine() call in the loop just reads that character in the first iteration.

So, you need the following -

int playernum = input.nextInt();
input.nextLine(); //read the unread new line character from the input stream
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • @Besh I gave it an up vote. How'd you know someone down voted your post? Is there a way to tell? I'm kind of new here. – Dummy Code Jul 02 '13 at 03:22
  • @HenryHarris You can see your downvote/upvote history under profile – Tdorno Jul 02 '13 at 03:24
  • @HenryHarris: Your rep goes down and also if you click on the number of votes on the left, it expands to show upvotes and downvotes. – Bhesh Gurung Jul 02 '13 at 03:25
  • @BheshGurung Oh dang. Thanks man. I can't see the number of upvotes/downvotes on the side of the voting... Weird. – Dummy Code Jul 02 '13 at 03:26
2

Use the following code. Comments in the code explain changes.

import java.util.Scanner;
import java.util.Random;
public class Run {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    Random rand = new Random();

    System.out.println("How many people will play???");
    int playernum = input.nextInt();
    input.nextLine(); //ADDED LINE

    String players[] = new String[playernum];

    for(int i = 0; i < playernum; i++){
        System.out.println("Who is player #" + (i+1)+"?");
        players[i] = input.nextLine();
    }

    System.out.println("The winner is: " + players[rand.nextInt(playernum)]);

}

}

We added input.nextLine(); because input.nextInt(); leaves a leftover new line character that we need to clear out. It was putting this new line character as player 1

-Henry

Dummy Code
  • 1,858
  • 4
  • 19
  • 38
  • Wouldn't it be better to help the OP figure out his issue and give him an approach to fix the problem versus just fixing it for him and moving on? – Tdorno Jul 02 '13 at 03:19
  • @Tdorno Good point. I tried to explain it but I guess it's better for him to learn on his own. – Dummy Code Jul 02 '13 at 03:23
0

I think your issue is on this line players[i] = input.nextLine();.

I think what you're looking for is, players[i] = input.next();

"Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end." See the API description here.

Jonathan
  • 589
  • 1
  • 13
  • 23
0

Use input.next() instead of input.nextLine() in the for loop. That way, the unused new line character won't be a problem, like @BheshGurung explains in his answer.

aksh1t
  • 5,410
  • 1
  • 37
  • 55