-
Notifications
You must be signed in to change notification settings - Fork 7
/
FileInput.java
52 lines (48 loc) · 1.25 KB
/
FileInput.java
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
/*
* File Input
*
* Take text file filename as argument and calculate the sum of the numbers.
* Each number should be on a seperate line.
*
* Use:
* $java FileInput numbers.txt
*
* numbers.txt:
* 1
* 2
* 3
* 4
* 5
* 6
*
* Output:
* Sum: 21.00
*/
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
public class FileInput {
public static void main(String[] args) {
try {
double sum = 0;
File file;
if (0 < args.length) {
String filename = args[0];
file = new File(filename);
} else {
System.out.println("Please enter filename");
return;
}
Scanner myReader = new Scanner(file);
while (myReader.hasNextLine()) {
double data = Double.parseDouble(myReader.nextLine());
sum += data;
}
myReader.close();
System.out.printf("Sum: %.2f\n", sum);
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}