Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.nio.file.Paths;
- import java.util.Scanner;
- public class IsItInTheFile {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- ArrayList<String> fileList = new ArrayList<>();
- System.out.println("Name of the file:");
- String file = scanner.nextLine();
- System.out.println("Search for:");
- String searchedFor = scanner.nextLine();
- try ( Scanner fileReader = new Scanner(Paths.get(file))) {
- while (fileReader.hasNextLine()) {
- fileList.add(fileReader.nextLine());
- }
- } catch (Exception e) {
- System.out.println("Reading the file" + file + " failed.");
- }
- int index = 0;
- while (index < fileList.size()) {
- String list = fileList.get(index);
- if (list.equals(searchedFor)) {
- System.out.println("Found!");
- } else {
- System.out.println("Not found.");
- }
- index++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement