Domanda

I'm working on a Chat Bot project, and I'm almost done, other than the fact that whenever I enter an input, it returns multiple outputs depending on the length of the input X.

Here is the source code:

import java.util.*;
public class ChatBot
{
public static String getResponse(String value)
{
    Scanner input = new Scanner (System.in);
    String X = longestWord(value);
    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?";
    }

    return "Now we are getting somewhere. How does " + X + " affect you the most?";
}

private static String longestWord(String value){
    Scanner input = new Scanner (value);
    String longest = new String();
    "".equals(longest);

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

}

This is for testing the Chat Bot:

import java.util.Scanner;


public class Test {
    public static void main (String [ ] args)
    {
    Scanner input = new Scanner (System.in);
    ChatBot e = new ChatBot();
    String prompt = "What would you like to talk about?";
    System.out.println(prompt);
    String userInput;
    userInput = input.next();
    while (!userInput.equals("Goodbye"))
    {
        System.out.println(e.getResponse(userInput));
        userInput = input.next();
    }

}
}

I am also trying to modify the Bot so it counts the number of times it has responded; and also modify it so it randomly returns a random response depending on the length of the input. Any help will be much appreciated. Thank You!
È stato utile?

Soluzione

You are using the Scanner.next method which only returns the next word in the string. So if you input a string with multiple words, your bot will respond to each of them.

You can use Scanner.nextLine() to get the entire input string, instead of only 1 word.


To count the number of times your bot has responded, you can create a field in the bot class:

private int responseCount = 0;

Then if you change yout getResponse method from a static method to an instance method, you can update this value from this method:

public String getResponse(String value)
{
    String X = longestWord(value);  //Your longestWord should also not be static.
    this.responseCount++;        

    if (value.contains("you"))
    {
        ...

Altri suggerimenti

Regarding counting the responses, just modify your main method:

import java.util.Scanner;

public class Test {
public static void main (String [ ] args)
{
   int numberOfResponses = 1;
   Scanner input = new Scanner (System.in);
   ChatBot e = new ChatBot();
   String prompt = "What would you like to talk about?";
   System.out.println(prompt);
   String userInput;
   userInput = input.next();
   while (!userInput.equals("Goodbye"))
   {
      System.out.println(e.getResponse(userInput));
      userInput = input.nextLine();
      numberOfResponses++;
   }
   input.close();
   System.out.println(numberOfResponses);
}
}

If I have the time I will edit my post in a few minutes to check your problem regarding the double appearences of a response. You also forgot to close the Scanner.

EDIT: It actually happens because scanner has as a default the delimiter set to be on whitespace. so if you input a text with a whitespace, the while loop runs twice for one user input. Just use the nextLine() command.

Why is this code:

 Scanner input = new Scanner (System.in);

In your getResponse method? Its not used at all. Take a closer look at your methods as they are holding some strange code.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top