6 changed files with 156 additions and 0 deletions
@ -0,0 +1,9 @@ |
|||||
|
public class InvalidInputException extends Exception { |
||||
|
InvalidInputException() { |
||||
|
super("Cannot handle empty input"); |
||||
|
} |
||||
|
|
||||
|
InvalidInputException(String input, Throwable cause) { |
||||
|
super(String.format("Cannot handle input: \"%s\"", input), cause); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,43 @@ |
|||||
|
import java.io.BufferedReader; |
||||
|
import java.io.FileNotFoundException; |
||||
|
import java.io.FileReader; |
||||
|
import java.io.IOException; |
||||
|
|
||||
|
public class MainSeven { |
||||
|
|
||||
|
private static int parseLine(String line) throws InvalidInputException { |
||||
|
String trimmed = line.trim(); |
||||
|
if (trimmed.isEmpty()) { |
||||
|
throw new InvalidInputException(); |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
return Integer.parseInt(trimmed); |
||||
|
} catch (NumberFormatException e) { |
||||
|
throw new InvalidInputException(trimmed, e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
try (FileReader fr = new FileReader("scores.txt")) { |
||||
|
BufferedReader br = new BufferedReader(fr); |
||||
|
|
||||
|
int sum = 0; |
||||
|
String line; |
||||
|
while ((line = br.readLine()) != null) { |
||||
|
try { |
||||
|
sum += parseLine(line); |
||||
|
} catch (InvalidInputException e) { |
||||
|
System.out.println(e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
br.close(); |
||||
|
System.out.println(sum); |
||||
|
} catch (FileNotFoundException e) { |
||||
|
System.out.println("File not found: " + e.getMessage()); |
||||
|
} catch (IOException e) { |
||||
|
System.out.println("IOException: " + e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
import java.io.BufferedReader; |
||||
|
import java.io.FileReader; |
||||
|
import java.io.IOException; |
||||
|
|
||||
|
public class MainSevenBefore { |
||||
|
public static void main(String[] args) throws IOException { |
||||
|
BufferedReader br = new BufferedReader(new FileReader("scores.txt")); |
||||
|
int sum = 0; |
||||
|
String line; |
||||
|
while ((line = br.readLine()) != null) { |
||||
|
sum += Integer.parseInt(line); |
||||
|
} |
||||
|
br.close(); |
||||
|
System.out.println(sum); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,57 @@ |
|||||
|
diff --git "a/.\\MainSevenBefore.java" "b/.\\MainSeven.java"
|
||||
|
index 5b72238..f8be922 100644
|
||||
|
--- "a/.\\MainSevenBefore.java"
|
||||
|
+++ "b/.\\MainSeven.java"
|
||||
|
@@ -1,16 +1,43 @@
|
||||
|
import java.io.BufferedReader; |
||||
|
+import java.io.FileNotFoundException;
|
||||
|
import java.io.FileReader; |
||||
|
import java.io.IOException; |
||||
|
|
||||
|
-public class MainSevenBefore {
|
||||
|
- public static void main(String[] args) throws IOException {
|
||||
|
- BufferedReader br = new BufferedReader(new FileReader("scores.txt"));
|
||||
|
- int sum = 0;
|
||||
|
- String line;
|
||||
|
- while ((line = br.readLine()) != null) {
|
||||
|
- sum += Integer.parseInt(line);
|
||||
|
+public class MainSeven {
|
||||
|
+
|
||||
|
+ private static int parseLine(String line) throws InvalidInputException {
|
||||
|
+ String trimmed = line.trim();
|
||||
|
+ if (trimmed.isEmpty()) {
|
||||
|
+ throw new InvalidInputException();
|
||||
|
+ }
|
||||
|
+
|
||||
|
+ try {
|
||||
|
+ return Integer.parseInt(trimmed);
|
||||
|
+ } catch (NumberFormatException e) {
|
||||
|
+ throw new InvalidInputException(trimmed, e);
|
||||
|
+ }
|
||||
|
+ }
|
||||
|
+
|
||||
|
+ public static void main(String[] args) {
|
||||
|
+ try (FileReader fr = new FileReader("scores.txt")) {
|
||||
|
+ BufferedReader br = new BufferedReader(fr);
|
||||
|
+
|
||||
|
+ int sum = 0;
|
||||
|
+ String line;
|
||||
|
+ while ((line = br.readLine()) != null) {
|
||||
|
+ try {
|
||||
|
+ sum += parseLine(line);
|
||||
|
+ } catch (InvalidInputException e) {
|
||||
|
+ System.out.println(e.getMessage());
|
||||
|
+ }
|
||||
|
+ }
|
||||
|
+
|
||||
|
+ br.close();
|
||||
|
+ System.out.println(sum);
|
||||
|
+ } catch (FileNotFoundException e) {
|
||||
|
+ System.out.println("File not found: " + e.getMessage());
|
||||
|
+ } catch (IOException e) {
|
||||
|
+ System.out.println("IOException: " + e.getMessage());
|
||||
|
} |
||||
|
- br.close();
|
||||
|
- System.out.println(sum);
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
96 |
||||
|
85 |
||||
|
74 |
||||
|
62 |
||||
|
|
||||
|
58 |
||||
|
y76 |
||||
@ -0,0 +1,24 @@ |
|||||
|
# 运行记录 |
||||
|
|
||||
|
## 正常情况 |
||||
|
|
||||
|
```shell |
||||
|
$ javac MainSeven.java && java MainSeven |
||||
|
Cannot handle empty input |
||||
|
Cannot handle input: "y76" |
||||
|
375 |
||||
|
``` |
||||
|
|
||||
|
## 文件移动或删除 |
||||
|
|
||||
|
```shell |
||||
|
$ javac MainSeven.java && java MainSeven |
||||
|
File not found: scores.txt (系统找不到指定的文件。) |
||||
|
``` |
||||
|
|
||||
|
## 删除文件读取权限 |
||||
|
|
||||
|
```shell |
||||
|
$ javac MainSeven.java && java MainSeven |
||||
|
File not found: scores.txt (拒绝访问。) |
||||
|
``` |
||||
Loading…
Reference in new issue