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)]);

}

}
2
  • 1
    THINK! You create an array of string based on playernum as it's size and then try to loop through something that increases... Commented Jul 2, 2013 at 3:04
  • 2
    possible duplicate of Scanner issue when using nextLine after nextXXX Commented Jul 2, 2013 at 3:08

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

4 Comments

@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.
@HenryHarris You can see your downvote/upvote history under profile<reputation<history
@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.
@BheshGurung Oh dang. Thanks man. I can't see the number of upvotes/downvotes on the side of the voting... Weird.
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

2 Comments

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 Good point. I tried to explain it but I guess it's better for him to learn on his own.
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.