Sal
Sal

Reputation: 109

Arrays and input

My assignment asks me to write a program that will let the user input 10 players' name, age, position, and batting average. The program should then check and display statistics of only those players who are under 25 years old and have a batting average of .280 or better, then display them in order of age.

I've written my code for the input section (where it'll store them in an array):

static int players[] = new int [10];
static String name[] = new String [10];
static double average [] = new double [10];
static int age[] = new int [10];
static String position[] = new String [10];

//method to input names of Blue Jays
public static void inputInfo() throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));       

    for(int i = 0; i < players.length; i++)
    {
        System.out.println("Enter player information.");

        System.out.println("Input first and last name: ");
        name [i] = br.readLine();

        System.out.println("Input position: ");
        position[i] = br.readLine();

        System.out.println("Input batting average (e.g. .246): ");
        String averageString = br.readLine();
        average [i] = Double.parseDouble(averageString);

        System.out.println("Input age: ");
        age[i] = br.read();

        System.out.println(" ");

    }   
}

My problem is the input. For the first player I input it shows me this (as it should):

Input first and last name: 
John Smith
Input position:
pitcher
Input batting average (e.g. .246): 
.300
Input age:
27

But my second input skips the name section completely and jumps to the position input. I can't really figure out why it's doing this! Can anyone help me out? Thanks in advance!

Upvotes: 1

Views: 201

Answers (3)

user1231232141214124
user1231232141214124

Reputation: 1349

read() only reads a single character at a time. When you press after entering your age, it appends a new line character to the end which triggers the .readLine() for name.

        System.out.println("Input age: ");
        age[i] = Integer.parseInt(br.readLine());

Upvotes: 1

Dennis Meng
Dennis Meng

Reputation: 5187

When you read in the age here:

System.out.println("Input age: ");
age[i] = br.read();

the newline from the user pressing <Enter> is still there. So, when you go back and do

System.out.println("Enter player information.");

System.out.println("Input first and last name: ");
name [i] = br.readLine();

the newline is still in the buffer and will be read in here.

Upvotes: 1

rgettman
rgettman

Reputation: 178333

The read method reads only a single character of input; the rest of the line you've entered remains in the stream.

When the next loop starts, readLine detects that it can read the rest of the line already, so it does with no user input. It thinks the user input is already given.

For the input age, use readLine instead of read, and you can use Double.parseDouble to convert the resulting String input to a double.

Upvotes: 8

Related Questions