-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise2.java
More file actions
30 lines (29 loc) · 866 Bytes
/
Copy pathExercise2.java
File metadata and controls
30 lines (29 loc) · 866 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
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Exercise2 {
public static void main(String[] args) {
FileInputStream fin = null;
try {
fin = new FileInputStream("src/Week5/input.txt");
int count = 0;
Scanner sc = new Scanner(fin);
while (sc.hasNext()) {
sc.next();
count++;
}
System.out.println("Number of words: " + count);
} catch (FileNotFoundException e) {
System.out.println("File not found");
} finally {
try {
if (fin != null) {
fin.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}