Please help! I need help modifying a program. The existing program does the foll
ID: 3635302 • Letter: P
Question
Please help! I need help modifying a program. The existing program does the following:- Three instance variables that are arrays of type String. Each array should hold at least 12 Strings. The three arrays will hold data read from three different files, described below.
- A method that opens three input files (named settings.txt, actions.txt and people.txt – see attachments) and reads their data into the 3 arrays (put actions in the action bag, etc.)
- A method that generates and prints out “headlines” consisting of strings pulled randomly from each of the bags, in the following order: people, actions, settings: in other words, randomly choose one people string, then randomly choose one action string, then randomly choose one settings string, and print them all out as one string.
- A main method that creates a Headlines object, prints out a headline, then prompts the user to see whether or not to continue; the program should continue to run as long as the user chooses to see more, and terminates when the user chooses to quit.
****** I would like to add the following capabilities:
- Keep track of which phrases in each file have been used, and don’t re-use them until all phrases have been used
- Allow the user to save any headlines s/he likes in a separate output file ******
Here is the existing code:
import java.io.*;
import java.util.Random;
public class HeadLines {
private String [] actions, people, settings;
private final int LENGTH = 12;
private int line;
public HeadLines()
{
line = 0;
actions = new String[LENGTH];
people = new String[LENGTH];
settings = new String[LENGTH];
}
public void readFiles() throws IOException{
String str;
FileReader fr = new FileReader("/Users/ribbit/Desktop/actions.txt");
//FileReader fr = new FileReader("actions.txt");
BufferedReader br = new BufferedReader(fr);
str = br.readLine();
while (str != null || line < LENGTH){
actions[line] = str;
line++;
str = br.readLine();
}
line = 0;
fr = new FileReader("/Users/ribbit/Desktop/people.txt");
//fr = new FileReader("people.txt");
br = new BufferedReader(fr);
str = br.readLine();
while (str != null || line < LENGTH){
people[line] = str;
line++;
str = br.readLine();
}
line = 0;
fr = new FileReader("/Users/ribbit/Desktop/settings.txt");
//fr = new FileReader("settings.txt");
br = new BufferedReader(fr);
str = br.readLine();
while (str != null || line < LENGTH){
settings[line] = str;
line++;
str = br.readLine();
}
line = 0;
}
public void printRandomSelection(){
Random rand = new Random();
int num1, num2, num3;
num1 = rand.nextInt(LENGTH);
System.out.print(people[num1] + " ");
num2 = rand.nextInt(LENGTH);
System.out.print(actions[num2] + " ");
num3 = rand.nextInt(LENGTH);
System.out.println(settings[num3] + ".");
}
}
///////////////////////////////////////
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
HeadLines hl = new HeadLines();
Scanner scan = new Scanner(System.in);
String response = "";
while (true) {
if (response.equals("N") || response.equals("n"))
{
System.out.println("Quitting...");
break;
}
hl.readFiles();
hl.printRandomSelection();
System.out.println("Want to play again? [y/n]");
response = scan.nextLine();
}
}
}
THANK YOU!!!!