Commit ffedd198 authored by Martin Deinhofer's avatar Martin Deinhofer
Browse files

formatted class

parent 1be8f416
......@@ -3,10 +3,8 @@ package java_exercises4_2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class MoneyDemo {
......@@ -15,35 +13,35 @@ public class MoneyDemo {
public static void main(String[] args) {
/*
* Always use try-with-resources statement, if possible. This way the files will be closed automatically
* in case of an exception and also if everything goes fine.
* Always use try-with-resources statement, if possible. This way the files will
* be closed automatically in case of an exception and also if everything goes
* fine.
*/
try (BufferedReader in = new BufferedReader(
Files.newBufferedReader(Paths.get(inputFileName)));
try (BufferedReader in = new BufferedReader(Files.newBufferedReader(Paths.get(inputFileName)));
PrintWriter out = new PrintWriter(Files.newBufferedWriter(Paths.get(outputFileName)))) {
double sum = 0;
String input;
//Read line by line
// Read line by line
while ((input = in.readLine()) != null) {
try {
//Sum up lines
// Sum up lines
sum += Double.parseDouble(input);
} catch (NumberFormatException e) {
//If the line is not parseable, skip it
// If the line is not parseable, skip it
System.out.println("NumberFormatException: Skipping line: " + input);
continue;
}
}
//Write out sum to the output file
// Write out sum to the output file
out.write("sum = " + sum);
System.out.println("Sum = " + sum);
} catch (IOException e) {
System.out.println("Could not read file: "+e.getMessage());
System.out.println("Could not read file: " + e.getMessage());
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment