Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a basic chat bot without using a scanner. All input will come from the command line. If the user entered nothing at the command

Create a basic chat bot without using a scanner. All input will come from the command line.
If the user entered nothing at the command line, show Nothing to say?
If the user entered some variation of how are you today? show Fine, but could use some more power. As long as the input has the word "are" before the word "you", print this message. Make your program smart enough to accept variations of these words (see output).
If the user entered some variation of what is your favorite something? Display something? I'm only interested in transistors.
In all other cases, show I didn't understand that. I can't pass all the test cases with the codes that I have.
Test Case 1
Passed!
Nothing to say?
Test Case 2
Passed!
Command Line arguments:
how are you today?
Fine, but could use some more power.
Test Case 3
Passed!
Command Line arguments:
how ARE yOU today?!?!
Fine, but could use some more power.
Test Case 4
Passed!
Command Line arguments:
You are not funny.
I didn't understand that.
Test Case 5
Passed!
Command Line arguments:
YOU ARE NOT FUNNY!
I didn't understand that.
Test Case 6
Passed!
Command Line arguments:
how are we able to make you happy?
Fine, but could use some more power.
Test Case 7
Passed!
Command Line arguments:
how are93 yYOu today
Fine, but could use some more power.
Test Case 8
Passed!
Command Line arguments:
how are how are we doing and you?
Fine, but could use some more power.
Test Case 9
Failed
Show what's missing
Command Line arguments:
what is your favorite cookie?
I didn't understand that.
Test Case 10
Passed!
Command Line arguments:
do you like cars?
I didn't understand that.
Test Case 11
Failed
Show what's missing
Command Line arguments:
what is your favorite car?
I didn't understand that.
Test Case 12
Failed
Show what's missing
Command Line arguments:
what favorite food?
I didn't understand that.
Test Case 13
Failed
Show what's missing
Command Line arguments:
what do I have to say to FIGURE OUT your FAVORITE3 soda?
I didn't understand that.
Test Case 14
Passed!
Command Line arguments:
i give up.
public static void main(String[] args)
{
// checking whether user entered anything in command line or not
if(args.length ==0)
System.out.println("Nothing to say?");
else
{
String input ="";
for(String data: args)
input = input + data +""; // appending all words of command line in a string
input = input.toLowerCase(); // changing whole string in lowercase , to make it case-insensitive
int indexOfAre = input.indexOf("are"); // getting the index of word "are"
int indexOfYou = input.indexOf("you"); // getting the index of word "you"
int indexOfFavourite = input.indexOf("favourite");// getting the index of word "favourite"
//checking if are is present before you or not ( if true then index of are is smaller than index of you)
if( indexOfAre !=-1 && indexOfYou !=-1 && indexOfAre < indexOfYou)
System.out.println("Fine, but could use some more power.");
// if string contains word favourite or not
else if(indexOfFavourite !=-1)
{
// getting the index of space after the word favourite
int spaceIndex = input.indexOf("", indexOfFavourite);
// getting the index of question mark (?)
int qmIndex = input.indexOf("?", spaceIndex);
// getting string between space and question mark after the word favourite
String str = input.substring(spaceIndex+1,qmIndex);
System.out.println(str.trim()+"? I'm only interested in transistors.");
}
else
System.out.println("I didn't understand that.");
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions