Robert Mutua
Robert Mutua

Reputation: 67

How can I make BufferedReader take multiple inputs at once?

My program requires that the console inputs both the user name and age at once. I am using a BufferedReader as shown. As you can see, addPassenger takes two inputs, name and age...but I can only put in name. How can that be implemented using a BufferedReader? In other words, how can I make "screenInput.readLine();" take both name and age as input strings? Any help appreciated.

public class Console {

public static void main(String[] args) {

    // Initialize database

    Database prodDB = new Database();
    prodDB.bootstrap();

    //Initialize console
    boolean always = true;
    BufferedReader screenInput = new BufferedReader(new InputStreamReader(System.in));

    while(always){

        //ask for passengerName and age, then add
        System.out.println("Enter Passenger Name and Age: ");

        String name = screenInput.readLine();

        boolean result = prodDB.addPassenger(name, age);

        if (result){
            System.out.println("Welcome back " + name);
        } else
        {
            System.out.println("Welcome " + name);
        }

        always = false;

Upvotes: 0

Views: 699

Answers (1)

dryairship
dryairship

Reputation: 6077

You can just add another line to take the age, like this:

String name = screenInput.readLine();
int age = Integer.parseInt(screenInput.readLine());

Upvotes: 1

Related Questions