Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.nio.file.Paths;
  4. import java.util.Scanner;
  5.  
  6. public class IsItInTheFile {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         ArrayList<String> fileList = new ArrayList<>();
  11.  
  12.         System.out.println("Name of the file:");
  13.         String file = scanner.nextLine();
  14.  
  15.         System.out.println("Search for:");
  16.         String searchedFor = scanner.nextLine();
  17.  
  18.         try ( Scanner fileReader = new Scanner(Paths.get(file))) {
  19.  
  20.             while (fileReader.hasNextLine()) {
  21.                 fileList.add(fileReader.nextLine());
  22.             }
  23.         } catch (Exception e) {
  24.             System.out.println("Reading the file" + file + " failed.");
  25.         }
  26.  
  27.         int index = 0;
  28.        
  29.         while (index < fileList.size()) {
  30.             String list = fileList.get(index);
  31.            
  32.             if (list.equals(searchedFor)) {
  33.                 System.out.println("Found!");
  34.             } else {
  35.                 System.out.println("Not found.");
  36.             }
  37.            
  38.             index++;
  39.         }
  40.  
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement