Java Arena
Step 3 of 5

Several values for each object

Most objects hold more than one value, and their constructors take several parameters. To create them from the user's input, the loop reads the values one after the other. The name comes first, because the empty line that ends the input comes where a name would:

Main.java
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Pet> pets = new ArrayList<>();

        while (true) {
            String name = scanner.nextLine();
            if (name.isEmpty()) {
                break;
            }
            int age = Integer.valueOf(scanner.nextLine());
            pets.add(new Pet(name, age));
        }

        for (Pet pet : pets) {
            System.out.println(pet);
        }
    }
}
Pet.java
public class Pet {
    private String name;
    private int age;

    public Pet(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return this.name;
    }

    public int getAge() {
        return this.age;
    }

    public String toString() {
        return this.name + ", " + this.age + " years old";
    }
}
Input
Rex
4
Misty
2
Kiwi
11
(an empty line)
Output
Rex, 4 years old
Misty, 2 years old
Kiwi, 11 years old
  • Each round reads a name and checks it first. Only when there is a pet does the program read its age, with Integer.valueOf(scanner.nextLine()).
  • new Pet(name, age) gives the values in the order of the constructor's parameters, Pet(String name, int age). new Pet(age, name) doesn't compile, since the first parameter must be a String.
  • The values are needed only to create the object, so they can live in variables that last one round.

Several values on one line

Data often comes with all of an object's values on one line, like Rex,4. Then each round reads one line and splits it, as in module 16:

while (true) {
    String line = scanner.nextLine();
    if (line.isEmpty()) {
        break;
    }
    String[] parts = line.split(",");
    pets.add(new Pet(parts[0], Integer.valueOf(parts[1])));
}

The constructor doesn't care where its values come from: typed on lines of their own, split from one line, or written in the program.

Your task

Challenge 1 of 3

A cinema takes bookings. Booking.java is ready. Read the bookings: for each one, the name of the person and then the row number. The input ends with an empty line where the next name would be. Create a Booking for each and add it to the list bookings, and then print every booking. For the input Ines, 12, Tom, 3 and an empty line, the program prints:

Ines: row 12
Tom: row 3

Challenge 2

Main.java reads podcasts, a title and then how many episodes the podcast has, until the title is an empty line. It doesn't compile yet, because Podcast only takes a title. Change Podcast so that its constructor takes the title and the number of episodes, the object keeps both, and a podcast prints like Night Sky (12 episodes). For the input Night Sky, 12, Word Play, 40 and an empty line, the program prints:

Night Sky (12 episodes)
Word Play (40 episodes)

Challenge 3

Each line of the input is a trip: a city and the number of days, separated by a comma, like Oslo,5. The input ends with an empty line. Trip.java is ready. Read the lines, split each one and add a Trip to the list trips. Then print every trip, and last Total: with the days of all the trips added up. For the input Oslo,5, Rome,3, Lima,4 and an empty line, the program prints:

Oslo, 5 days
Rome, 3 days
Lima, 4 days
Total: 12 days