Question

Just one last part remaining in the ChatBot. I need to figure out a way to modify the chatbot class so that it occasionally (say, 30% of the time) returns a randomly-­‐generated standard reply to user input one of at least five possible replies, like “LOL”, “OMG”, “You don’t say”, “Really?”, or “I see”.

Edit: Applied recommended changes:

import java.util.Random;
import java.util.Scanner;
public class ChatBot 
{
private int responseCount = 0;
public String getResponse(String value)
{
    String X = longestWord(value);
    this.responseCount++;
    if (responseCount == 10)
    {
        return "Sorry, but our time is up. I can't talk with you any longer.";
    }
    if (value.contains("you"))
    {
        return "I'm not important. Let's talk about you instead.";
    }


    else if (X.length() <= 3)
    {
        return "Maybe we should move on. Is there anything else you would like to talk about?";
    }
    else if (X.length() == 4)
    {
        return "Tell me more about " + X;
    }

    else if (X.length() == 5)
    {
        return "Why do you think " + X + " is important?";
    }
    else if (X.length() <=9)
    {
    return "Now we are getting somewhere. How does " + X + " affect you the most?";
    }

    return getRandomResponse();
}


public String longestWord(String value){
    Scanner input = new Scanner (value);
    String longest = new String();
    longest = "";

    while (input.hasNext())
    {
        String temp = input.next();
        if(temp.length() > longest.length())
        {
            longest = temp;
        }
    }
    return longest;
}

private String getRandomResponse()
{

String [] responses = {"OMG", "LOL", "You don't say", "Really?", "I See"};

return responses [(int)(Math.random() * responses.length)];
}
}

The problem is, it keeps returning the same response, instead of one of the five responses given. Any help would me much appreciated, thank you!

Edit:It's now giving only the random responses, and overriding every other response in the getResponse() method.

Was it helpful?

Solution

Given your logic, your getRandomResponse method should always return "OMG". This is because on the first run of the loop in that method, counter = 1. Thus the first if statement will run and will return "OMG" exitting the method. A nicer equivalent might putting all teh responses into an array and returning a random value from it, rather than doing somehting strange with iteration:

String[] responses = {"OMG", "LOL", "You don't say", "Really?", "I See"};
return responses[(int)(Math.random() * responses.length)];

OTHER TIPS

In getRandomResponse, you make a random number generator using Random(), but you never use it. Then in your for loop, you execute your decision-making tree but use a variable counter that always begins at 0. Then on the first time through your loop, the first if statement will execute because 0 < 5, so "OMG" is returned.

Edit: I just noticed something else that is not going to work in your code:

Random randomNumber = new Random();
for (int counter =0; counter<10; counter++)
{
    counter = randomNumber.nextInt();

You're trying to use counter to do two different things: you are trying to run this loop 10 times, but you're also using it to store random values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top