Java Arena
Step 7 of 8

Processing many lines

Real data comes in many lines. The while (true) loop from module 8, which reads until a signal to stop, works here too. This time the signal is an empty line, and each line before it is split and used:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int total = 0;
        int highest = 0;
        String priciest = "";
        while (true) {
            String line = scanner.nextLine();
            if (line.isEmpty()) {
                break;
            }
            String[] parts = line.split(",");
            int price = Integer.valueOf(parts[1]);
            total += price;
            if (price > highest) {
                highest = price;
                priciest = parts[0];
            }
        }
        System.out.println("Total: " + total);
        System.out.println("Most expensive: " + priciest + " (" + highest + ")");
    }
}
Input
bread,3
cheese,7
milk,2
(an empty line)
Output
Total: 12
Most expensive: cheese (7)
  • line.isEmpty() is true for the empty line at the end of the input, where the user pressed Enter without typing anything, and break ends the loop. line.equals("") does the same, but line == "" isn't reliable, for the reasons in the step on comparing.
  • The total and the highest price are updated in every round. Each line is used once and then forgotten, so the program needs no list.
  • To know which product was the most expensive, its name is kept in a variable of its own, updated together with the price.

Starting highest at 0 works because prices are never negative. For values that can be below zero, such as temperatures, start from a number that no real value can beat, or from the first line's value.

Other ways to stop

The signal to stop can be anything the data never contains. Instead of an empty line, it's often a word such as end, checked with line.equals("end").

When the lines are already in an array, there's nothing to read and no signal to wait for: a for-each loop goes through the lines, and its body is the same as above.

for (String line : lines) {
    String[] parts = line.split(",");
    int price = Integer.valueOf(parts[1]);
    // ... the same as in the while loop
}

Your task

Challenge 1 of 3

Read lines in the form runner,minutes until the line end. Then print how many runners there were, how many of them finished in under an hour (less than 60 minutes), and their average time as a decimal number. For the input Aino,42, Bruno,65, Chen,49 and end the program prints:

Runners: 3
Under an hour: 2
Average: 52.0

Challenge 2

Write a method coldest(String[] lines) that gets lines in the form city,temperature and returns the coldest city with its temperature in parentheses. The temperatures are whole numbers, and some can be below zero. The check runs this and expects the output below:

String[] lines = {"Lisbon,14", "Oslo,-3", "Rome,11"};
System.out.println(coldest(lines));
Oslo (-3)

Challenge 3

Read an order, one line per item in the form item,price,count, until the line end. For each item, print its name, the count and what that line costs. At the end, print the total of the whole order. For the input pen,2,3, notebook,4,2, bag,15,1 and end the program prints:

pen x3 = 6
notebook x2 = 8
bag x1 = 15
Total: 29