-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFileInputFun.java
More file actions
33 lines (28 loc) · 962 Bytes
/
Copy pathFileInputFun.java
File metadata and controls
33 lines (28 loc) · 962 Bytes
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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class FileInputFun {
public static void main(String[] args) {
Scanner infile;
try {
infile = new Scanner(new File("input.txt"));
int input;
int sum = 0;
while(infile.hasNext()) {
input = infile.nextInt();
System.out.println(input);
sum += input; //sum = sum + input
}//end while
System.out.println("Sum is " + sum);
infile.close();
}
catch(FileNotFoundException ex) {
System.out.println("Can't find file!");
System.out.println(ex.getMessage());
}//end try-catch
catch(InputMismatchException ex) {
System.out.println("Error reading input!");
}
}//end main
}