Records in a file
Most data files hold records: each line describes one thing, with its values separated by a comma. This format is called CSV, short for comma-separated values, and spreadsheet programs can save a sheet in it. Each line is read, split and converted, just as in module 16:
Ilona,3
Marek,1
Sofia,5import java.nio.file.Paths;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int total = 0;
int longest = 0;
String longestName = "";
try (Scanner scanner = new Scanner(Paths.get("rentals.txt"))) {
while (scanner.hasNextLine()) {
String[] parts = scanner.nextLine().split(",");
String name = parts[0];
int hours = Integer.valueOf(parts[1]);
System.out.println(name + ": " + hours + " h");
total += hours;
if (hours > longest) {
longest = hours;
longestName = name;
}
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
System.out.println("Total: " + total + " h");
System.out.println("Longest rental: " + longestName);
}
}
Ilona: 3 h
Marek: 1 h
Sofia: 5 h
Total: 9 h
Longest rental: Sofiascanner.nextLine().split(",") reads a line and splits it at once. The parts are Strings, so the hours need Integer.valueOf before they can be added up.
Not every file uses commas. Semicolons are common too, especially where the comma is the decimal separator: then the line is split with split(";"). Look at the file before you write the program.
A header line
A CSV file often starts with a header: a first line that names the columns, such as name,hours. It isn't data, and Integer.valueOf("hours") would fail. Read it before the loop, and don't use it:
try (Scanner scanner = new Scanner(Paths.get("rentals.csv"))) {
scanner.nextLine(); // skip the header
while (scanner.hasNextLine()) {
String[] parts = scanner.nextLine().split(",");
// ...
}
}
Lines that don't fit
A line with no comma splits into one part only, and parts[1] throws an exception. Inside the try, that ends the reading. When a file may have such lines, check parts.length first, and skip the line with continue if it has too few parts.