-3

When I run my program my for loop outputs the line "Enter a game score?" twice before I am able to input sometimes, have a made a silly error while constructing it.

import java.util.Scanner;

public class Project {

public static void main (String[] args){

Scanner scan = new Scanner(System.in);
System.out.println("What is your name?");   
    String playername = scan.nextLine();


    System.out.println("How many games will you be entering?"); 
    int numofgames = scan.nextInt();

    /* Creating the arrays*/
    String[] game = new String[numofgames];
    int[] scores = new int [numofgames];
    int[] mins = new int [numofgames];

    /* Creating a temp array to store the game data before splitting*/
    String[] temparray = new String[numofgames];

    /*Getting the users game data and storing it into the temp array*/
    for (int count=1; count <= numofgames; count++){
        System.out.println("Enter a game score?(Game name:Scores:Mins)");
                temparray[count] = scan.nextLine();


    }}}
dasish54
  • 1
  • 3

1 Answers1

0

The function scan.nextInt() does not discard the newline character after you hit 'Enter'. So it stays in the buffer and is read when scan.nextLine() is called. Just empty the buffer after scan.nextInt()

int numofgames = scan.nextInt();
scan.nextLine(); // remove newline from buffer

You should also fix your loop boundaries:

for (int count=0; count < numofgames; count++) {
...
}