linhe 2 months ago
parent
commit
24ae14ab1b
Signed by: linhe GPG Key ID: 3113E644F1D5D722
  1. 9
      w7/InvalidInputException.java
  2. 43
      w7/MainSeven.java
  3. 16
      w7/MainSevenBefore.java
  4. 57
      w7/refactor.diff
  5. 7
      w7/scores.txt
  6. 24
      w7/usage.md

9
w7/InvalidInputException.java

@ -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);
}
}

43
w7/MainSeven.java

@ -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());
}
}
}

16
w7/MainSevenBefore.java

@ -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);
}
}

57
w7/refactor.diff

@ -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);
}
}

7
w7/scores.txt

@ -0,0 +1,7 @@
96
85
74
62
58
y76

24
w7/usage.md

@ -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…
Cancel
Save