Method is undefined for class TestChatBot?

123 Views Asked by At
      import java.util.*;

public class TestChatBot 
{
    public static void main(String [] args)
    {
        Scanner input = new Scanner(System.in);
        String x = input.nextLine();


        TestChatBot e = new TestChatBot();
        {
            String prompt = "What would you like to talk about?";
            System.out.println(prompt);

            String userInput = input.nextLine();

            while(!userInput.equals("Goodbye"))
            {
                System.out.println(e.getResponse());
                userInput = input.nextLine();
            }
        }

    }

public class ChatBot 
{

    public String getResponse(String input)
    {
        Scanner userInput = new Scanner(input);
        input = userInput.nextLine();

        longestWord(input);

        String keyword = "you";
        int you = input.indexOf(keyword);

        if (you >= 0)
            return "I'm not important. Let's talk about you.";

        if (input.length() <= 3)
            return "Maybe we should move on. Is there anything else you would like to 
                        talk about?";

        if (input.length() == 4)
            return "Tell me more about " + input;

        if(input.length() == 5)
            return "Why do you think " + input + "is important?";

        else 
            return "Now we're getting somewhere. How does " + input + "affect you the 
                most?";
    }

    private String longestWord(String x)
    {
        Scanner input = new Scanner(x);     

        String longest = "";

        String temp = input.next();

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

        return longest;

        }




         }
    }

In my ChatBotTest class it says that my getResponse() method is undefined for the class TestChatBot... I don't really understand why it says this and it's preventing my code from running. I'm pretty new to Java so I'm sorry for poor/sloppy coding. Any help is greatly appreciated, thank you!

1

There are 1 best solutions below

1
On
TestChatBot e = new TestChatBot();

Should be

ChatBot e = new ChatBot();

TestChatBox has no getResponse()

Also, your getResponse takes a String argmument. I think you want to pass userInput to it

System.out.println(e.getResponse(userInput));