(See the next iteration.)
I have this command line program for computing GPA (grade point average). Given credit sequence \$\langle c_1, c_2, \dots, c_n \rangle\$ and grade sequence \$\langle g_1, g_2, \dots, g_n \rangle\$ (I assume European grades, i.e., \$g_i \in \{ 1, 2, 3, 4, 5 \}\$ for all \$i \in \{1, 2, \dots, n \} \$), grade point average is
$$\frac{\sum_{i = 1}^n c_i g_i}{\sum_{i = 1}^n c_i}.$$
Being invoked without arguments, the program will expect input from keyboard. With one argument (which must be a CSV file with two columns), the program computes the GPA, prints it, and exits.
I tried to make the program robust: it reports all errors without crashing (except whenever the input file does not exist).
App.java
package net.coderodde.gpa;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* This class implements a command line application for computing GPA (grade
* point average).
*
* @author Rodion "rodde" Efremov
* @version 1.6 (Mar 20, 2016)
*/
public class App {
private final List<Integer> creditList = new ArrayList<>();
private final List<Integer> gradeList = new ArrayList<>();
private final Scanner scanner;
public App(String fileName) {
if (fileName == null) {
scanner = new Scanner(System.in);
} else {
try {
scanner = new Scanner(new File(fileName));
} catch (FileNotFoundException ex) {
throw new RuntimeException(
"File \"" + fileName + "\" does not exist.", ex);
}
}
}
public static void main(String[] args) {
if (args.length > 1) {
printHelp();
return;
}
try {
App app = new App(args.length == 0 ? null : args[0]);
app.populateLists();
System.out.println(app.computeGradePointAverage());
} catch (Exception ex) {
System.err.println("ERROR: " + ex.getMessage());
System.exit(1);
}
}
private void populateLists() {
int lineNumber = 1;
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (line.equals("end")) {
return;
}
if (line.isEmpty()) {
lineNumber++;
continue;
}
if (!line.contains(",")) {
System.err.println(
"Line " + lineNumber + " does not have a comma.");
lineNumber++;
continue;
}
String[] parts = line.split(",");
if (parts.length != 2) {
System.err.println(
"Line " + lineNumber + " contains invalid number of " +
"tokens (" + parts.length + "). Should have exactly " +
"two: number of credits and the grade.");
} else {
String creditsString = parts[0].trim();
String gradeString = parts[1].trim();
int credits;
int grade;
try {
credits = Integer.parseInt(creditsString);
} catch (NumberFormatException ex) {
System.err.println(
"Invalid credit token on line " + lineNumber +
": " + creditsString);
++lineNumber;
continue;
}
try {
grade = Integer.parseInt(gradeString);
} catch (NumberFormatException ex) {
System.err.println(
"Invalid grade token on line " + lineNumber +
": "+ gradeString);
++lineNumber;
continue;
}
creditList.add(credits);
gradeList.add(grade);
}
lineNumber++;
}
}
private double computeGradePointAverage() {
double creditSum = 0.0;
double sum = 0.0;
for (int i = 0; i < creditList.size(); ++i) {
creditSum += creditList.get(i);
sum += creditList.get(i) * gradeList.get(i);
}
return sum / creditSum;
}
private static void printHelp() {
System.out.println("Usage: java -jar FILE.jar [grades.csv]");
System.out.println("Omit the only argument in order to input " +
"data from command line.");
}
}
Please, tell me anything that comes to mind.