Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a Java program that: Reads a user-specified text file into an ArrayList of Strin

ID: 653774 • Letter: A

Question

a Java program that:

Reads a user-specified text file into an ArrayList of Strings

Displays a menu of options to the user:

Searches for a user-specified word/phrase (String) in the document (ArrayList of Strings)
if the word/phrase is found, the line (String) containing the word/phrase is displayed
otherwise, a message (e.g., "hello" not found) is displayed

Prints (displays) the document on the screen

Searches for a user-specified word/phrase (String) in the document (ArrayList of String)
if the word/phrase is found, replaces it with another user-specified word/phrase
otherwise, a message (e.g., "hello" not found) is displayed

Quits (exits) the program

Explanation / Answer

FileSearch.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

public class FileSearch {

   static ArrayList<String> stringList = new ArrayList<>();

private static String filePath;

   public static void main(String args[]) {

       Scanner input = new Scanner(System.in);

       System.out.println("Enter File Path");

       filePath = input.next();

       updateArrayList();

       while (true) {

           System.out.println(" Press 0 to exit");

           System.out

                   .println("Press 1 to search and display a String in the file");

           System.out.println("Press 2 to display compelete file");

           System.out.println("Press 3 to search and replace the string ");

           int option = input.nextInt();

           switch (option) {

           case 1:

               System.out.println("Please Enter the word you want to search");

               String searchString = input.next();

               isFileContainsString(searchString);

               break;

           case 2:

               printFile();

               break;

           case 3:

               System.out

                       .println("Please Enter the String you want to replace");

               String oldString = input.next();

               System.out

                       .println("Please Enter the New String you want to replace with Old String");

               String newString = input.next();

               replaceString(newString, oldString);

               break;

           case 0:

               System.out.println("Program exited");

               return;

           default:

               System.out.println("Invalid option");

           }

       }

   }

   public static void isFileContainsString(String searchString) {

       if (stringList.contains(searchString)) {

           System.out.println("The word " + searchString

                   + " is found in the file");

       } else {

           System.out.println("The word " + searchString

                   + " is not found in the file");

       }

   }

   public static void updateArrayList() {

       Scanner sc2 = null;

       try {

           sc2 = new Scanner(

                   new File(filePath));

       } catch (FileNotFoundException e) {

           e.printStackTrace();

       }

       while (sc2.hasNextLine()) {

           Scanner s2 = new Scanner(sc2.nextLine());

           while (s2.hasNext()) {

               String s = s2.next();

               stringList.add(s);

           }

       }

   }

   public static void printFile() {

       System.out.println(stringList);

   }

   public static void replaceString(String newString, String oldString) {

       for (int i = 0; i < stringList.size(); i++) {

           if (stringList.get(i).equalsIgnoreCase(oldString)) {

               stringList.set(i, newString);

           }

       }

   }

}

Sample test:

Enter File Path

/Users/smohammed/Desktop/translate.txt

Press 0 to exit

Press 1 to search and display a String in the file

Press 2 to display compelete file

Press 3 to search and replace the string

1

Please Enter the word you want to search

lol

The word lol is found in the file

Press 0 to exit

Press 1 to search and display a String in the file

Press 2 to display compelete file

Press 3 to search and replace the string

2

[lol, i, am, hungry, bitch, lol, lol, lol, bitch]

Press 0 to exit

Press 1 to search and display a String in the file

Press 2 to display compelete file

Press 3 to search and replace the string

3

Please Enter the String you want to replace

lol

Please Enter the New String you want to replace with Old String

yahoo

Press 0 to exit

Press 1 to search and display a String in the file

Press 2 to display compelete file

Press 3 to search and replace the string

2

[yahoo, i, am, hungry, bitch, yahoo, yahoo, yahoo, bitch]

Press 0 to exit

Press 1 to search and display a String in the file

Press 2 to display compelete file

Press 3 to search and replace the string

3

Please Enter the String you want to replace

bitch

Please Enter the New String you want to replace with Old String

dog

Press 0 to exit

Press 1 to search and display a String in the file

Press 2 to display compelete file

Press 3 to search and replace the string

2

[yahoo, i, am, hungry, dog, yahoo, yahoo, yahoo, dog]

Press 0 to exit

Press 1 to search and display a String in the file

Press 2 to display compelete file

Press 3 to search and replace the string

0

Program exited

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote