Java Arena
Step 2 of 7

Totals, flags and joined text

In the last module you added up a list with an index loop. With a for-each loop it's shorter, because there's no index to keep track of:

ArrayList<Integer> pages = new ArrayList<>();
pages.add(12);
pages.add(30);
pages.add(18);

int total = 0;
for (int count : pages) {
    total += count;
}
System.out.println("Pages read: " + total);
System.out.println("Per day: " + (double) total / pages.size());
Output
Pages read: 60
Per day: 20.0

Averages and counts work just as before; only the loop is new. This step adds two more things a loop over a list often has to work out: a yes-or-no answer about all the values, and one line of text made from them.

Does every value pass? A flag

Some questions are about the whole list: is every value valid? Is any value negative? A boolean variable answers them. It's called a flag: it starts with the answer that holds until some value proves otherwise, and the loop changes it when a value does.

ArrayList<Integer> ages = new ArrayList<>();
ages.add(19);
ages.add(23);
ages.add(17);
ages.add(30);

boolean allAdults = true;
for (int age : ages) {
    if (age < 18) {
        allAdults = false;
    }
}

if (allAdults) {
    System.out.println("Everyone is 18 or over");
} else {
    System.out.println("Someone is under 18");
}
Output
Someone is under 18

One age under 18 is enough to make the flag false, and nothing sets it back to true. That's why the if in the loop has no else: with else { allAdults = true; }, only the last age would decide, and here the 30 would hide the 17.

For "is there any ...?", turn it around: start the flag at false, and set it to true when a value matches.

Joining values into one line

Text can be built in a loop, as in module 11: start from "" and add each value with +=. To put a separator between the values, but not in front of the first one, check whether the text is still empty:

ArrayList<String> stops = new ArrayList<>();
stops.add("Home");
stops.add("Park");
stops.add("School");

String route = "";
for (String stop : stops) {
    if (!route.equals("")) {
        route += " > ";
    }
    route += stop;
}
System.out.println(route);
Output
Home > Park > School

In the first round route is still empty, so no separator goes in. From then on, each stop gets one in front of it.

Your task

Challenge 1 of 3

A delivery van takes parcels of at most 25 kg each, and at most 100 kg in all. The program reads parcel weights until 0, into the list parcels. Add one for-each loop that adds up the weights and keeps a flag that says whether every parcel is 25 kg or less. Print the total, and then Ready to go when both rules hold, or Repack the van when either one is broken. For the input 20, 18, 25 and 9 the program prints:

Total: 72 kg
Ready to go

Challenge 2

Read whole numbers until 0, and print them as one addition with its result: the numbers with + between them, then = and the sum. Build the line with a for-each loop. There's always at least one number. For the input 4, 7 and 2 the program prints:

4 + 7 + 2 = 13

Challenge 3

Read the names of your dinner guests until the line end (there's at least one). Print them on one line the way you'd write them in a sentence: a comma between the names, but and before the last one. For the input Ada, Ben, Cleo and Dev the program prints:

Guests: Ada, Ben, Cleo and Dev

Two guests are Ada and Ben, and a single guest is just the name.