Question
There seems to be an error when a question that ends with '?' is entered. Error Exception in thread main java.lang.NullPointerException at project2.PromptBank.replaceCode(PromptBank.java:39) at project2.PromptBankTester.main(PromptBankTester.java:32)
There seems to be an error when a question that ends with '?' is entered.
Error Exception in thread "main" java.lang.NullPointerException at project2.PromptBank.replaceCode(PromptBank.java:39) at project2.PromptBankTester.main(PromptBankTester.java:32)
Please fix error.
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 sc = new Scanner(System.in); PromptBank pc = new PromptBank(); pc.populateStatementsArray(); System.out.println("ELIZA:Hello, my name is Eliza. What is your name?"); System.out.print("USER:"); String name = ""; while(name.isEmpty()){ name = sc.nextLine(); if(!name.isEmpty()) break; } System.out.println("ELIZA:Hello, "+name+" Tell me what is on your mind today in 1 sentence."); boolean flag=true; while(flag){ System.out.print("USER:"); String val = ""; while(val.isEmpty()){ val = sc.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("ELIZA: "+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("ELIZA: 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("ELIZA: "+repl); }else if(val.equalsIgnoreCase("EXIT")){ System.out.println("Do you want run this session again:"); String ans = ""; while(ans.isEmpty()){ ans = sc.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("ELIZA: "+repl); } } } public static int getRandomNumber(){ Random rn = new Random(); int randomNum = rn.nextInt(2) + 0; return randomNum; } }
0 File Edit Scurce Refatr Navigate Search Preject Rur Winduw Help Quick Access : E | PromptBank , replacecodestrina, 5tnna, string) : String cterminateds PromptBankTrsteraa Applir 180 151 1 package rt2 public class ProqutBank S String 1 stotents- 1 packag project2 s public class PrtBa Tester Stri 3]: ulic static voidlnStrig a Scannr s-men Sca nerSystes.in) public Prcsptank B questions-nc Strinst 1: initialize your array to te ccerect length to istatesents-initialize your anray to the cerrect length to natchyoreb is Eliza. iat is yor n Systs.out.printi"ELIZA Hello,y Syste.out.printi USER: String nae - 2 pablic void spulatstatcnetaArray)l 14 statments/11 . "eLAA:1 sedia iportant to you, "w does BLAHK2. Please tell ne t 15 statments"2] _ "eLAAKnd eLAK2 sees to be on your nind. Let's talk sous it. 1s complete tis methed with your other statcnents i BLANK2 for wardi Systs.out.printi"ELIZA Helo, Tl1 ne hat is on your nind tocay in boelean fiag-true whileflaa) Syste.out.printi USER: String val - wileval.isEpty 22 q-estions/11 "Does eLAAK bot.er you? Ho ab-ut eu.'K2 ?", 25 conplete tis ethod with your ter questions using BLAk1 for wordi if(!val.isEspty) ifval.ensith 31 public Strinz fimtwcrdString -al) 2 retur val.solit [e1 rpc.replacacod- BLANK2', Inord, repl) Syste.out.printiELIA: r pbli Strinz lastnord Stri l s Strin-a1.splitf" i retur al ls.length ] String word pc.fimtwrdi al): pblic Strin String reol- pc.replaceode BLANK", fwond,.statoaents octRachr1 rpc.replacacod- BLAN2 Inord, repl) Syste.out.printiELI2A: N! Catic reol: String word pc.fimtwrdi al): String reol- pc.replaceode BLANK", fwond,.statoaents octRachr1 ropl "BLAMK2 Inord, repl: Syst.out.printi"ELI2A: r Systen. out .print1n("Do you want run this session agin."); outline Wntacle Smart Inse3288 E Updating Sotware (23%) 1:16 AM O Type here to search dx ENG 12/10/2017Step 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