Can someone help me figure out what to code in this Java code?

I am currently working on an assignment where I have to create a chatbot to respond to several statements like the ones listed in the instructions below, but I need help on figuring out how to write that in. I am using Java and the Magpie class to figure this out and I really need help on what to do because I don’t know how to write in the statements or the responses. Please help!

The instructions for the chatbot:
** * * * Have it respond “Tell me more about your pets” when the statement contains the word “dog” or “cat”. For example, a possible statement and response would be:

  • Statement: I like my cat Mittens.
  • Response: Tell me more about your pets.
  • Have it respond favorably when it sees the name of your teacher. Be sure to use appropriate pronouns! For example, a possible statement and response would be:
    • Statement: Mr. Finkelstein is telling us about robotics.
    • Response: He sounds like a good teacher.**

My code so far:

public class Magpie2
{
   public String getGreeting()
   {
     return "Hello, let's talk.";
   }

   public String getResponse(String statement)
   {
     String response = "";
     if (statement.indexOf("no") >= 0) {
       response = "Why so negative?";
     } else if (statement.indexOf("mother") >= 0
                 || statement.indexOf("father") >= 0
                 || statement.indexOf("sister") >= 0
                 || statement.indexOf("brother") >= 0) {
                 response = "Tell me more about your family.";
     } else {
       response = getRandomResponse();
     }
     return response;
   }

   private String getRandomResponse()
   {
     final int NUMBER_OF_RESPONSES = 4;
     double r = Math.random();
     int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
     String response = "";

     if (whichResponse == 0) {
       response = "Interesting, tell me more.";
     } else if (whichResponse == 1) {
       response = "Hmmm.";
     } else if (whichResponse == 2) {
       response = "Do you really think so?";
     } else if (whichResponse == 3) {
       response = "You don't say.";
     }
     return response;
         }

   public static void main(String[] args)
   {
     Magpie2 maggie = new Magpie2();

     System.out.println(maggie.getGreeting());
     System.out.println(">My mother and I talked last night.");
     System.out.println(maggie.getResponse("My mother and I talked last night."));
     System.out.println(">I said no.");
     System.out.println(maggie.getResponse("I said no!"));
     System.out.println(">The weather is nice.");
     System.out.println(maggie.getResponse("The weather is nice."));
     System.out.println(">Do you know my brother?");
     System.out.println(maggie.getResponse("Do you know my brother?"));
   }
}

Hello!

The class is showing you how to write the statements (maggie.getResponse("My mother and I talked last night."), the String is a statement) and the responses (if (statement.indexOf("mother") is part of how the response is determined), so what seems to be the problem :slight_smile:?

I am not sure how to get my code to create a statement and have the chatbot reply to that specific statement. Can you help?

The statements, in this case, are nothing else than made up Strings to test the responses of your Magpie2.getResponse method.

What you should be doing is simply searching for the keywords passed to the Magpie2.getResponse method.

As the instructions say, if in your main function you wrote this:

String simulatedUserStatement = "I like my cat Mittens";
System.out.println("> " + simulatedUserStatement);

Passing simualtedUserStatement to Magpie2.getResponse should return:

Tell me more about your pets

So, inside your getResponse method you should be searching for the keyword and creating the appropriate response:

int index = statement.indexOf("cat");
String response = "";
if (index >= 0) response = "Tell me more about about your pets";

return response;

Does it help?

I think so. I will try it out later.

1 Like

Would I do the same for the teacher statement and response?

Yes, but instead of dog or cat, you should be searching (indexOf) for the name of your teacher.

Wait, I’m not quite sure where I would place all of that code.

The statements go inside your public static void main method (you create them, simulating a person that’s chatting), while the one that analyzes the statement and creates the response goes inside the getResponse.

Did you mean like this?

 public class Magpie2
{
   public String getGreeting()
   {
     return "Hello, let's talk.";
   }

   public String getResponse(String statement)
   {
     String response = "";
     if (statement.indexOf("no") >= 0) {
       response = "Why so negative?";
     } else if (statement.indexOf("mother") >= 0
                 || statement.indexOf("father") >= 0
                 || statement.indexOf("sister") >= 0
                 || statement.indexOf("brother") >= 0) {
                 response = "Tell me more about your family.";
                 int index = statement.indexOf("cat");
                 String response = "";
                if (index >= 0) response = "Tell me more about about your pets";

return response;
     } else {
       response = getRandomResponse();
     }
     return response;
   }

   private String getRandomResponse()
   {
     final int NUMBER_OF_RESPONSES = 4;
     double r = Math.random();
     int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
     String response = "";

     if (whichResponse == 0) {
       response = "Interesting, tell me more.";
     } else if (whichResponse == 1) {
       response = "Hmmm.";
     } else if (whichResponse == 2) {
       response = "Do you really think so?";
     } else if (whichResponse == 3) {
       response = "You don't say.";
     }
     return response;
         }

   public static void main(String[] args)
   {
     Magpie2 maggie = new Magpie2();

     System.out.println(maggie.getGreeting());
     System.out.println(">My mother and I talked last night.");
     System.out.println(maggie.getResponse("My mother and I talked last night."));

     System.out.println(">I said no.");
     System.out.println(maggie.getResponse("I said no!"));

     System.out.println(">The weather is nice.");
     System.out.println(maggie.getResponse("The weather is nice."));

     System.out.println(">Do you know my brother?");
     System.out.println(maggie.getResponse("Do you know my brother?"));

     String simulatedUserStatement = "I like my cat Mittens";
     System.out.println("> " + simulatedUserStatement);
   }
}

Kind of :stuck_out_tongue:.

Inside your main method, only the simulatedUserStatement is correct because you’re not testing for the other ones (unless you didn’t paste the entire assignment).

You should remove everything inside the getResponse method, except for what you just added. Try to think about what will happen:

String response = "";
     if (statement.indexOf("no") >= 0) {
       response = "Why so negative?";
     } else if (statement.indexOf("mother") >= 0
/* 
Are you expecting any of the 
statements to contain mother/father/sister/brother?
If not, then this block will never run, and if does, 
it will return the wrong response.
*/
                 || statement.indexOf("father") >= 0
                 || statement.indexOf("sister") >= 0
                 || statement.indexOf("brother") >= 0) {
                 response = "Tell me more about your family.";
     } else {
       response = getRandomResponse();
     }
     return response;

Your code is inside the right function though :stuck_out_tongue:.

Okay I changed my code to create the statement “I like my cat Mittens.” but I don’t know how to get the chatbot code to respond with “Tell me more about your pets.” Can you help me?
This is my code right now

public class Magpie2
{
   public String getGreeting()
   {
     return "Hello, let's talk.";
   }

   public String getResponse(String statement)
   {
     String response = "";
     if (statement.indexOf("no") >= 0) 
     {
       response = "Why so negative?";
     } 
     else if (statement.indexOf("mother") >= 0
                 || statement.indexOf("father") >= 0
                 || statement.indexOf("sister") >= 0
                 || statement.indexOf("brother") >= 0
                 || statement.indexOf("cat") >= 0) 
     {
       response = "Tell me more about your family.";
       
     } 
     else 
     {
       response = getRandomResponse();
     }
     return response;
   }

   private String getRandomResponse()
   {
     final int NUMBER_OF_RESPONSES = 4;
     double r = Math.random();
     int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
     String response = "";

     if (whichResponse == 0) 
     {
       response = "Interesting, tell me more.";
     } 
     else if (whichResponse == 1) 
     {
       response = "Hmmm.";
     } 
     else if (whichResponse == 2) 
     {
       response = "Do you really think so?";
     } 
     else if (whichResponse == 3) 
     {
       response = "You don't say.";
     }
     return response;
         }

   public static void main(String[] args)
   {
     Magpie2 maggie = new Magpie2();

     System.out.println(maggie.getGreeting());
     System.out.println(">My mother and I talked last night.");
     System.out.println(maggie.getResponse("My mother and I talked last night."));
     System.out.println(">I said no.");
     System.out.println(maggie.getResponse("I said no!"));
     System.out.println(">The weather is nice.");
     System.out.println(maggie.getResponse("The weather is nice."));
     System.out.println(">Have you met my brother?");
     System.out.println(maggie.getResponse("Have you met my brother?"));
       
     //Cat
     System.out.println(">I like my cat Mittens.");
     System.out.println(maggie.getResponse("I like my cat Mittens."));
   }
}

Let’s remove the content that’s not needed:

public class Magpie2
{
  public String getGreeting ()
  {
    return "Hello, let's talk.";
  }

  public String getResponse (String statement)
  {
    String response = "";
    
    // Start again.
    // Which keywords are you looking for?
    // How do you find a string inside another?
    // String.indexOf: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int)
  }

  public static void main (String[]args)
  {
    Magpie2 maggie = new Magpie2();
    System.out.println (maggie.getGreeting());

    // You have a keyword in the comments.
    // What should you be looking for inside
    // getResponse?
    // Cat
    System.out.println (">I like my cat Mittens.");
    System.out.println (maggie.getResponse("I like my cat Mittens."));
  }
}

I am looking for the keywords “dog” and “cat” and I need the chatbot to respond with “tell me more about your pets” but I don’t know to find a string inside of another string.

Yes, but that’s why I pointed you to the documentation :slight_smile:. That’s one way to search for a String inside another. You could also search online how to find a string inside another java.

I’m trying to make you split the problem into smaller pieces to find a solution by yourself :slight_smile:.

I’ll show you how to respond only when there’s one keyword, the rest is up to you:

public class Magpie2
{
  public String getGreeting ()
  {
    return "Hello, let's talk.";
  }

  public String getResponse (String statement)
  {
    String response = "";
    
    // Start again.
    // Which keywords are you looking for?
    // How do you find a string inside another?
    // String.indexOf: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int)

    if (statement.indexOf("cat") >= 0) {
      response = "Tell me more about your pets";
    }

    return response;
  }

  public static void main (String[]args)
  {
    Magpie2 maggie = new Magpie2();
    System.out.println (maggie.getGreeting());

    // You have a keyword in the comments.
    // What should you be looking for inside
    // getResponse?
    // Cat
    System.out.println (">I like my cat Mittens.");
    System.out.println (maggie.getResponse("I like my cat Mittens."));
  }
}

Can you please just show me how to get the chatbot to respond? I am unsure and this assignment is due in 2 days. I’m really sorry to be so pushy it’s just this thing is due in 2 days and I am really stressed.

I tried this and I am getting a bunch of errors. Can you please help?

public class Magpie2
{
   public String getGreeting()
   {
     return "Hello, let's talk.";
   }

   public String getResponse(String statement)
   {
     String response = "";
     if (statement.indexOf("no") >= 0) 
     {
       response = "Why so negative?";
     } 
     else if (statement.indexOf("mother") >= 0
                 || statement.indexOf("father") >= 0
                 || statement.indexOf("sister") >= 0
                 || statement.indexOf("brother") >= 0
                 || statement.indexOf("cat") >= 0
                 || statement.indexOf("Mr.") >= 0
                 || statement.indexOf("Ms.") >= 0
                 || statement.indexOf("Mrs.") >= 0)
         if (statement.indexOf("cat") >= 0) 
         {
           response = "Tell me more about your pets";
    }

    return response;
  }
     {
       response = "Tell me more about your family.";
       
     } 
     
     else 
     {
       response = getRandomResponse();
     }
     return response;
   }

   private String getRandomResponse()
   {
     final int NUMBER_OF_RESPONSES = 4;
     double r = Math.random();
     int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
     String response = "";

     if (whichResponse == 0) 
     {
       response = "Interesting, tell me more.";
     } 
     else if (whichResponse == 1) 
     {
       response = "Hmmm.";
     } 
     else if (whichResponse == 2) 
     {
       response = "Do you really think so?";
     } 
     else if (whichResponse == 3) 
     {
       response = "You don't say.";
     }
     return response;
         }

   public static void main(String[] args)
   {
     Magpie2 maggie = new Magpie2();

     System.out.println(maggie.getGreeting());
     System.out.println(">My mother and I talked last night.");
     System.out.println(maggie.getResponse("My mother and I talked last night."));
     System.out.println(">I said no.");
     System.out.println(maggie.getResponse("I said no!"));
     System.out.println(">The weather is nice.");
     System.out.println(maggie.getResponse("The weather is nice."));
     System.out.println(">Do you know my brother?");
     System.out.println(maggie.getResponse("Do you know my brother?"));
     //Cat
     System.out.println(">I like my cat Mittens.");
     System.out.println(maggie.getResponse("I like my cat Mittens."));
       
     //Dog
     System.out.println(">I like my dog Ace.");
     System.out.println(maggie.getResponse("I like my cat Ace."));
       
     //Mr. Teacher Pronouns
     System.out.println(">Mr. Finkelstein is telling us about robotics.");
     System.out.println(maggie.getResponse("Mr. Finkelstein is telling us about robotics."));
     
     //Ms. Teacher Pronouns
     System.out.println(">Ms. Finkelstein is telling us about robotics.");
     System.out.println(maggie.getResponse("Ms. Finkelstein is telling us about robotics."));
     
     //Mrs. Teacher Pronouns
     System.out.println(">Mrs. Finkelstein is telling us about robotics.");
     System.out.println(maggie.getResponse("Mrs. Finkelstein is telling us about robotics."));
    
   }
}

Okay wait nevermind, I’ve got it.

1 Like

Hey, I have a question that stems from my earlier response. Is it okay to copy and paste code when you have a job as a programmer? I am a junior in high school and I want to be a cybersecurity analyst but I don’t have any coding experience and the Java course that I’m taking right now is hard. I feel like I wouldn’t be a good programmer because I don’t know what I’m doing and I’m always looking for instant answers instead of how to actually code myself. Do you know anyone that’s like that? I feel really bad. What can I do to get better at this?