-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadFile.java
More file actions
28 lines (26 loc) · 777 Bytes
/
Copy pathReadFile.java
File metadata and controls
28 lines (26 loc) · 777 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
import java.util.*;
import java.io.*;
public class ReadFile {
void countWordsAndLines(String file) {
int word_count = 0;
int line_count = 0;
try {
Scanner sc = new Scanner(new File(file));
while (sc.hasNextLine()) {
Scanner sc2 = new Scanner(sc.nextLine());
while (sc2.hasNext()) {
word_count++;
sc2.next();
}
line_count++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(word_count);
System.out.println(line_count);
}
public static void main(String[] args) {
new ReadFile().countWordsAndLines("test.txt");
}
}