-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearchingWord_from_file.java
30 lines (30 loc) · 1.04 KB
/
searchingWord_from_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
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FindingWordFromFile {
public static void main(String args[]) throws FileNotFoundException {
//Reading the word to be found from the user
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter the word to be found");
String word = sc1.next();
boolean flag = false;
int count = 0;
System.out.println("Contents of the line");
//Reading the contents of the file
Scanner sc2 = new Scanner(new FileInputStream("D:\sampleData.txt"));
while(sc2.hasNextLine()) {
String line = sc2.nextLine();
System.out.println(line);
if(line.indexOf(word)!=-1) {
flag = true;
count = count+1;
}
}
if(flag) {
System.out.println("File contains the specified word");
System.out.println("Number of occurrences is: "+count);
} else {
System.out.println("File does not contain the specified word");
}
}
}