Question
When I 'exit' my code, I would like it to loop again from the beginning. Please fix code. CODE package project2; public class PromptBank {
When I 'exit' my code, I would like it to loop again from the beginning. Please fix code.
CODE
package project2;
public class PromptBank { String [] questions = new String[3]; String [] statements = new String[3]; public PromptBank(){ //questions = new String[ ]; //initialize your array to the correct length to match your number of questions you populate it with //statements = //initialize your array to the correct length to match your number of questions you populate it with } public void populateStatementsArray(){ statements[0] = "Tell me more about BLANK1 and BLANK2"; statements[1] = "BLANK1 seems important to you, so does BLANK2. Please tell me more."; statements[2] = "BLANK1 and BLANK2 seem to be on your mind. Let's talk about it."; /*complete this method with your other statements using BLANK1 for word1 * and BLANK2 for word2 place holder */ } public void populateQuestionsArray(){ questions[0] = "Is there anything else about BLANK1 and BLANK2?"; questions[1] = "Does BLANK1 bother you? How about BLANK2?"; questions[2] = "Are BLANK1 and BLANK2 things you think about often?"; /*complete this method with your other questions using BLANK1 for word1 * and BLANK2 for word2 place holder */ } public String firstword(String val){ return val.split(" ")[0]; } public String lastword(String val){ String vals[] = val.split(" "); return vals[vals.length-1]; } public String replaceCode(String val,String replacedStr,String mainStr){ return mainStr.replaceAll(val, replacedStr); } }
TESTER
package project2;
import java.util.Random; import java.util.Scanner; public class PromptBankTester { public static void main(String args[]){ Scanner hive = new Scanner(System.in); PromptBank pc = new PromptBank(); pc.populateStatementsArray(); pc.populateQuestionsArray();
System.out.println(" WELCOME TO THE HIVE "); System.out.println("HELLO. I AM THE RED QUEEN. WHAT IS YOUR NAME?"); System.out.print("USER:");
String name = "";
while(name.isEmpty()){ name = hive.nextLine(); if(!name.isEmpty()) break; }
System.out.println("RED QUEEN: HELLO, "+name+". WHAT IS ON YOUR MIND TODAY?");
boolean flag=true;
while(flag){ System.out.print(name+": ");
String val = "";
while(val.isEmpty()){ val = hive.nextLine(); if(!val.isEmpty()) break; }
if(val.endsWith("?")) { String fword = pc.firstword(val); String lword = pc.lastword(val);
String repl = pc.replaceCode("BLANK1 ", fword, pc.questions[getRandomNumber()]); repl = pc.replaceCode("BLANK2 ", lword, repl);
System.out.println("RED QUEEN: "+repl);
}
else if(val.endsWith("!")){ String fword = pc.firstword(val); String lword = pc.lastword(val);
String repl = pc.replaceCode("BLANK1 ", fword, pc.statements[getRandomNumber()]); repl = pc.replaceCode("BLANK2 ", lword, repl);
System.out.println("RED QUUEN: WOW! Dramatic! "+repl); }
else if(val.endsWith("."))
{ String fword = pc.firstword(val); String lword = pc.lastword(val);
String repl = pc.replaceCode("BLANK1 ", fword, pc.statements[getRandomNumber()]); repl = pc.replaceCode("BLANK2 ", lword, repl);
System.out.println("RED QUEEN: "+repl); }
else if(val.equalsIgnoreCase("EXIT"))
{ System.out.println("Do you want run this session again:"); String ans = "";
while(ans.isEmpty()){ ans = hive.nextLine(); if(!ans.isEmpty()) break; }
if(ans.equalsIgnoreCase("no")){ System.out.println("Goodbye, until next time"); flag = false; break; } }else{
String fword = pc.firstword(val); String lword = pc.lastword(val); String repl = pc.replaceCode("BLANK1", fword, pc.statements[getRandomNumber()]); repl = pc.replaceCode("BLANK2", lword, repl); System.out.println("RED QUEEN: "+repl); } } }
public static int getRandomNumber(){ Random rn = new Random(); int randomNum = rn.nextInt(2) + 0; return randomNum; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started