Using the Design Recipe and the NetBeans IDE, write a Java program that: Reads a
ID: 653535 • Letter: U
Question
Using the Design Recipe and the NetBeans IDE, write 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
---------------------------------------------------------------
Assignment Notes
It is strongly recommended that you use stepwise refinement, and define a method for each of the menu items (except for the quit option).
To read a text file into the ArrayList of Strings, use:
A while loop the Scanner method hasNextLine()
In the while loop body, call the Scanner method nextLine(), and add the returned String to the ArrayList of Strings.
The String method indexOf() returns the position of the String parameter as a value of the type int. If the String parameter is not found in the String for which the indexOf method is called, the value -1 is returned.
To replace one substring with another in a String, you can use the String member function replace().
-------------------------------------------------------
Sample Run (user input italicized)
Input file name? README.txt
Select an option:
f/F: search the document for a word/phrase
p/P: print the document on the screen
q/Q: quit the program
r/R: replace a word/phrase in the document
s/S: save the document to a file
Enter your choice: p
========================
BUILD OUTPUT DESCRIPTION
========================
When you build an Java application project that has a main class, the IDE
automatically copies all of the JAR
files on the projects classpath to your projects dist/lib folder. The IDE
also adds each of the JAR files to the Class-Path element in the application
JAR files manifest file (MANIFEST.MF).
To run the project from the command line, go to the dist folder and
type the following:
java -jar "IfTest.jar"
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
Notes:
* If two JAR files on the project classpath have the same name, only the first
JAR file is copied to the lib folder.
* Only JAR files are copied to the lib folder.
If the classpath contains other types of files or folders, these files (folders)
are not copied.
* If a library on the projects classpath also has a Class-Path element
specified in the manifest,the content of the Class-Path element has to be on
the projects runtime path.
* To set a main class in a standard Java project, right-click the project node
in the Projects window and choose Properties. Then click Run and enter the
class name in the Main Class field. Alternatively, you can manually type the
class name in the manifest Main-Class element.
Select an option:
f/F: search the document for a word/phrase
p/P: print the document on the screen
q/Q: quit the program
r/R: replace a word/phrase in the document
s/S: save the document to a file
Enter your choice: s
Output file name? README2.txt
Select an option:
f/F: search the document for a word/phrase
p/P: print the document on the screen
q/Q: quit the program
r/R: replace a word/phrase in the document
s/S: save the document to a file
Enter your choice: q
Input file name? README2.txt
Select an option:
f/F: search the document for a word/phrase
p/P: print the document on the screen
q/Q: quit the program
r/R: replace a word/phrase in the document
s/S: save the document to a file
Enter your choice: p
========================
BUILD OUTPUT DESCRIPTION
========================
When you build an Java application project that has a main class, the IDE
automatically copies all of the JAR
files on the projects classpath to your projects dist/lib folder. The IDE
also adds each of the JAR files to the Class-Path element in the application
JAR files manifest file (MANIFEST.MF).
To run the project from the command line, go to the dist folder and
type the following:
java -jar "IfTest.jar"
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
Notes:
* If two JAR files on the project classpath have the same name, only the first
JAR file is copied to the lib folder.
* Only JAR files are copied to the lib folder.
If the classpath contains other types of files or folders, these files (folders)
are not copied.
* If a library on the projects classpath also has a Class-Path element
specified in the manifest,the content of the Class-Path element has to be on
the projects runtime path.
* To set a main class in a standard Java project, right-click the project node
in the Projects window and choose Properties. Then click Run and enter the
class name in the Main Class field. Alternatively, you can manually type the
class name in the manifest Main-Class element.
Select an option:
f/F: search the document for a word/phrase
p/P: print the document on the screen
q/Q: quit the program
r/R: replace a word/phrase in the document
s/S: save the document to a file
Enter your choice: q
Explanation / Answer
Complete Program:
// File: TestArrayList.java
import java.io.*;
import java.util.*;
public class TestArrayList
{
public static void main(String[] args) throws FileNotFoundException
{
ArrayList<String> list = new ArrayList<String>();
Scanner input = new Scanner(System.in);
char choice;
readFromFile(list, input);
do
{
displayMenu();
System.out.print("Enter your choice: ");
choice = input.nextLine().charAt(0);
switch(choice)
{
case 'f':
case 'F':
search(list, input);
break;
case 'p':
case 'P':
print(list);
break;
case 'q':
case 'Q':
writeToFile(list, input);
System.out.println("Thank you!");
break;
case 'r':
case 'R':
replace(list, input);
break;
case 's':
case 'S':
writeToFile(list, input);
break;
default:
System.out.println("Invalid choice!");
}
}while(choice != 'q' && choice != 'Q');
}
public static void search(ArrayList<String> list, Scanner input)
{
System.out.print(" Enter a word/phrase: ");
String str = input.nextLine();
boolean isfound = false;
for(int i = 0; i < list.size(); i++)
{
if(list.get(i).contains(str))
{
System.out.println(list.get(i));
isfound = true;
}
}
if(!isfound)
System.out.println(""" + str + "" not found" );
}
public static void replace(ArrayList<String> list, Scanner input)
{
System.out.print(" Enter a word/phrase: ");
String str = input.nextLine();
System.out.print("Enter a new word/phrase: ");
String newStr = input.nextLine();
boolean isfound = false;
for(int i = 0; i < list.size(); i++)
{
if(list.get(i).contains(str))
{
newStr = list.get(i).replace(str, newStr);
list.remove(i);
list.add(i, newStr);
isfound = true;
}
}
if(!isfound)
System.out.println(""" + str + "" not found" );
}
public static void print(ArrayList<String> list)
{
System.out.println();
for(int i = 0; i < list.size(); i++)
{
System.out.println(list.get(i));
}
}
public static void readFromFile(ArrayList<String> list, Scanner input) throws FileNotFoundException
{
System.out.print(" Input file name? ");
String inputFileName = input.nextLine();
Scanner infile = new Scanner(new File(inputFileName));
while(infile.hasNextLine())
{
String line = infile.nextLine();
list.add(line);
}
infile.close();
}
public static void writeToFile(ArrayList<String> list, Scanner input) throws FileNotFoundException
{
System.out.print(" Input file name? ");
String inputFileName = input.nextLine();
PrintWriter outfile = new PrintWriter(new File(inputFileName));
for(int i = 0; i < list.size(); i++)
{
outfile.write(list.get(i) + " ");
}
outfile.close();
}
public static void displayMenu()
{
System.out.println();
System.out.println("Select an option:");
System.out.println(" f/F: search the document for a word/phrase");
System.out.println(" p/P: print the document on the screen");
System.out.println(" q/Q: quit the program");
System.out.println(" r/R: replace a word/phrase in the document");
System.out.println(" s/S: save the document to a file");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.