Question
Could you help me implement the functionality described below to the code I wrote below? thanks If the user's response contains words of the form
Could you help me implement the functionality described below to the code I wrote below? thanks
If the user's response contains words of the form "I feel like
input:I feel like quitting my job.
output:Why do you feel like quitting your job?
Consider using String object methods, including indexOf(), .substring(), and .replace().
My code below already has some functionalities
1. Ask the user if they want to talk
2. If they don't say goodbye
3. If they do, respond to what they say, and go to step 1
4.
If the user repeats something they've said the last time, say "You said that last time." If they repeat something they said one time before , say "You said that the time before last."
If the user's response contains the word "awesome," say "It would be "awesome" if you expanded your vocabulary."
Below are my code.
package Jan31;
import java.util.Scanner;
public class sadjnsjdas
{
static final boolean DBG = false;
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
final StringNULL_STRING = "";
Stringanswer,prev1 = NULL_STRING, prev2 = NULL_STRING;
do
{
System.out.print("Talk to me, or hit enter to quit: ");
answer = keyboard.nextLine();
if (answer.equals(NULL_STRING) )
{
break;
}
// If the user repeats something they've said the last time, say
// "You said that last time."If they repeat something that said one time
// before that, say "You said that the time before last.
if (answer.equalsIgnoreCase(prev1))
{
System.out.println("You said that last time");
continue;
}
else if (answer.equalsIgnoreCase(prev2))
{
System.out.println("You said that the time before last");
continue;
//update prev1 and prev2 for the checks in the next iteration.
/*
* What we expect to happen
* 1: answer = "Anita", prev1 = NULL_STRING, prev2 = NULL_STRING
* 2: answer = "Bernie",prev1 = "Anita", prev2 = NULL_STRING
* 2: answer = "Chris", prev1 = "Bervie",prev2 = Anita
*/
prev2 = prev1;
prev1 = answer;
// If the user's response contains the word "awesome,"
// say "It would be "awesome" if you expanded your vocabulary."
}
String ANSWER = answer.toUpperCase();
if ( ANSWER.indexOf("AWESOME") >= 0)
{ //if a case insensitive search for AWESOME in answer succeeds
String s ="It would be \"awesome\" if you expanded your vocabulary.";
System.out.println(s);
continue;
}//if awesome
//if the sentence contains more than 1 !, say
//"You are way too excitable!" and go back to the top of the loop.
int exclaim_count=0;
for (int index=0;index < answer.length() ; index++ )
{
if (answer.charAt(index) == '!')
{
++exclaim_count;
}
}
if (exclaim_count > 1)
{
System.out.println("You are way too excitable!");
continue;
} //if
char lastChar = answer.charAt(answer.length()-1);
if (DBG)
{
System.out.printf("DBG: lastChar==%c ",lastChar);
}
switch (lastChar)
{
case '?':
if (answer.length() % 2 == 0) //the length is even
{
System.out.println("Yes, of course");
}
else
{
System.out.println("No");
}
break;
case '!':
System.out.println("Wow");
break;
case '.':
System.out.printf("You always say \"%s\" ",answer);
break;
default:
System.out.println("I demand you speak in complete sentences!!");
} //switch
}
while (true); //(!answer.equals(NULL_STRING) );
System.out.println("Goodbye and good luck!");
}//main()
}//class Conversation
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