Last active
May 16, 2020 14:12
-
-
Save dan-dm/44e8dec67bab857f7436a5574511c79b to your computer and use it in GitHub Desktop.
Created with Copy to Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
src/main/java/IsItInTheFile.java | |
import java.nio.file.Paths; | |
import java.util.Scanner; | |
public class IsItInTheFile { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Name of the file:"); | |
String file = scanner.nextLine(); | |
System.out.println("Search for:"); | |
String searchedFor = scanner.nextLine(); | |
int lines = 0; | |
try (Scanner fileReader = new Scanner(Paths.get(file))) { | |
while (fileReader.hasNextLine()) { | |
String line = fileReader.nextLine(); | |
if (!line.contains(searchedFor)) { | |
continue; | |
} | |
lines++; | |
} | |
} catch (Exception e) { | |
System.out.println("Reading the file " + file + " failed."); | |
} | |
if (lines == 0) { | |
System.out.println("Not found."); | |
} else { | |
System.out.println("Found!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment