-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path15.Count_word_in_file.java
33 lines (33 loc) · 1.1 KB
/
15.Count_word_in_file.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
/*Question - Traverse a file and count the words “CSE1007” and “JAVA”, print the counts in the console.*/
//Ans:
import java.io.*;
import java.util.regex.*;
public class Traverse_File_19BCE2250 {
public static void main(String[] args) {
String w = "";
try {
FileReader fileR = new FileReader("File_Location");
int num;
while ((num = fileR.read()) != -1) {
w = w + (char) num;
}
} catch (Exception e) {
System.out.print("error in reading the file");
}
Pattern C1 = Pattern.compile("CSE1007");
Matcher M1 = C1.matcher(w);
int CSE1007_count = 0;
while (M1.find()){
CSE1007_count++;
}
Pattern C2 = Pattern.compile("JAVA");
Matcher M2 = C2.matcher(w);
int JAVA_count = 0;
while (M2.find()){
JAVA_count++;
}
System.out.println(w);
System.out.println("the total number of words CSE1007 is " + CSE1007_count);
System.out.println("the total number of words JAVA is " + JAVA_count);
}
}