-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
53 lines (38 loc) · 1.5 KB
/
Copy pathMain.java
File metadata and controls
53 lines (38 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.io.*;
public class Main {
public static void main(String[] args) {
String fileName = "ArithmeticExpressions.txt";
String line = null;
try {
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
ACalculator calculator = new ACalculator();
double result = ACalculator.evaluate(line);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("Arithmetic_Output.txt", true));
writer.append(line + " = " + result + "\n");
writer.close();
}
catch(IOException e) {
System.out.println("Error writing to file");
}
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}