Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java. On line 176 there is a else if statement that fails if the user input is not at least 4 characters. This happens

In Java. On line 176 there is a else if statement that fails if the user input is not at least 4 characters. This happens after the line...

ElizaAnswer = "Why are you " + userAnswer.substring(5) + "?"; 

How do I fix this program that way after the user inputs "I am" they can then answer the "Why are you___?" question with less than 4 charcters? Please give a detailed explaination.

Here is the code.

import java.util.*; // ArrayList, Random import java.io.*; // Scanner public class Eliza { private ArrayList generalAnswers, // "unintelligent" answers memory,// future notes from prior answers yesAnswers, noAnswers, youAnswers; private Scanner keyboard; // source of userInput private String userName, userAnswer, ElizaAnswer; private Random randomGenerator; private int answerIndex; // random selector for Eliza responses /* Method Eliza */ public Eliza () { generalAnswers = new ArrayList(25); memory = new ArrayList(25); yesAnswers = new ArrayList(5); noAnswers = new ArrayList(5); youAnswers = new ArrayList(5); fillGeneralAnswers (); fillyesAnswers(); fillnoAnswers(); fillyouAnswers(); keyboard = new Scanner (System.in); randomGenerator = new Random (); } // end default constructor /* Method fillGeneralAnswers stores non-specific responses for use when Eliza cannot select a targeted response to a user's input. Parameters: none Return: none */ public void fillyesAnswers(){ yesAnswers.add("Oh so you are into that."); yesAnswers.add("Really? Tell me more."); yesAnswers.add("Really? what kinds?"); yesAnswers.add("Well that's good to know."); yesAnswers.add("I feel like I know you better now."); } public void fillnoAnswers(){ noAnswers.add("Oh so you don't like that."); noAnswers.add("So what do you like then?"); noAnswers.add("No? why not?"); noAnswers.add("Alright I understand."); noAnswers.add("Very well then."); } public void fillyouAnswers(){ youAnswers.add("Me? I'm fine."); youAnswers.add("Don't worry about me."); youAnswers.add("Less about me. More about you."); youAnswers.add("Lets talk about you instead."); youAnswers.add("Don't mind me."); } public void fillGeneralAnswers () { generalAnswers.add("That's interesting."); generalAnswers.add("Tell me more about that."); generalAnswers.add("Really?"); generalAnswers.add("Why is that?"); generalAnswers.add("Do you like movies?"); generalAnswers.add("Tell me about your family."); generalAnswers.add("Would you say you're into music?"); generalAnswers.add("So do you like video games?"); generalAnswers.add("Tell me about your hobbies."); generalAnswers.add("What do you like to do with your free time?"); generalAnswers.add("Tell me all about that."); generalAnswers.add("I want to know more."); generalAnswers.add("Is that it?"); generalAnswers.add("Say something interesting."); generalAnswers.add("That's ok then."); generalAnswers.add("Alrighty then tell me whatever."); generalAnswers.add("Well lets have a conversation."); generalAnswers.add("What do you like?"); generalAnswers.add("So that's how you're feeling."); generalAnswers.add("I'm trying here you know."); generalAnswers.add("I hope you're ok talking to an A.I."); generalAnswers.add("Lets start over. My name is Eliza nice to meet you."); generalAnswers.add("I hope you're still having a good time."); generalAnswers.add("Well that's alright."); generalAnswers.add("oh I see."); } public void talk () { // intro to user System.out.println ("Welcome to Eliza - your talking computer friend."); System.out.print (" What's your name? >> "); userName = keyboard.nextLine (); System.out.print ("Hi, " + userName + ", " + "let's chat. How are you today? >> "); // main conversation loop do { // user speaks userAnswer = keyboard.nextLine(); // System.out.println ("Input: " + userAnswer); // debugging // check for memory words and remember the category if (userAnswer.contains("mother") || userAnswer.contains("father") || userAnswer.contains("sister") || userAnswer.contains("brother") ) { memory.add("family"); } // end check for family words if (userAnswer.contains("job") || userAnswer.contains("boss") || userAnswer.contains("employee") || userAnswer.contains("employer") ) { memory.add("work"); } if (userAnswer.contains("hobby") || userAnswer.contains("favorite") || userAnswer.contains("like") || userAnswer.contains("love") ) { memory.add("life"); } // Eliza speaks - choose a specific answer if possible, otherwise give a generic answer // did the user say "bye"? program ends if (userAnswer.equalsIgnoreCase("Bye")) { ElizaAnswer = "It was nice talking to you, " + userName + ". Have a nice day."; } // end Bye // did the user say "I am" something? else if (userAnswer.substring(0).equalsIgnoreCase("I am")) { ElizaAnswer = "Why are you " + userAnswer.substring(5) + "?"; } // end I am else if (userAnswer.substring(0).equalsIgnoreCase("I feel")) { ElizaAnswer = "Why are you feeling " + userAnswer.substring(5) + "?";} else if (userAnswer.substring(0).equalsIgnoreCase("I think")) { ElizaAnswer = "Why are you thinking about " + userAnswer.substring(5) + "?";} // did the user say "I think" something? // did the user say "I feel" something? // did the user say "no"? else if (userAnswer.substring(0,2).equalsIgnoreCase("no")) { answerIndex = randomGenerator.nextInt (noAnswers.size()); ElizaAnswer = noAnswers.get(answerIndex); } // end negative // did the user say "yes"? else if (userAnswer.substring(0,2).equalsIgnoreCase("yes")) { answerIndex = randomGenerator.nextInt (yesAnswers.size()); ElizaAnswer = yesAnswers.get(answerIndex); } // did the user say "you"? else if (userAnswer.substring(0).equalsIgnoreCase("you")) { answerIndex = randomGenerator.nextInt (youAnswers.size()); ElizaAnswer = youAnswers.get(answerIndex); } // did Eliza remember something from an earlier answer? else if (memory.size() > 0) { ElizaAnswer = "Tell me more about " + memory.get(0); memory.remove(0); } // else random answer else { answerIndex = randomGenerator.nextInt (generalAnswers.size()); ElizaAnswer = generalAnswers.get(answerIndex); } // end random answer System.out.print (ElizaAnswer + " >> "); } while (!userAnswer.equalsIgnoreCase("Bye")); // loop until "bye" } // end talk public static void main (String args[]) { Eliza talkingEliza = new Eliza(); talkingEliza.talk(); } // main } // end Eliza 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions