-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindInputString.java
43 lines (37 loc) · 1.31 KB
/
FindInputString.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
34
35
36
37
38
39
40
41
42
43
package file;
import java.io.File;
import java.io.FileNotFoundException;
public class FindInputString {
/**
* Main method of the class for the following question:
* Find Input String in the File.
* */
public static void main(String[] args) {
java.util.Scanner scanner = new java.util.Scanner(System.in);
System.out.print("Enter Input String: ");
String input = scanner.nextLine();
File file = new File("src/file/file.txt");
findInputStringInFile(file, input);
scanner.close();
}
private static void findInputStringInFile(File file, String input) {
try {
java.util.Scanner scanner = new java.util.Scanner(file);
var lineCounter = 0;
var isFound = false;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
lineCounter++;
if (line.contains(input)) {
System.out.println("Input String Found in File on Line: " + lineCounter);
isFound = true;
break;
}
}
if (!isFound)
System.out.println("String Not found on file.");
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
}
}
}