How to create a chatbot using a loop? (java)

1.1k Views Asked by At

I'm trying to create a chatbot using a loop that will have a conversation with the user, and will stop when the user types "bye". Kind of like what i'm trying to do here, except i suck at programming:

    Scanner sc = new Scanner (System.in);
    String question;

    System.out.println("Hello");

    do
    {
        question = sc.nextLine();

        if (question.equals("how are you");
        {
            System.out.println("good");
        }

        if (question.equals("bye"))
        {
            System.out.println("bye");
            break;
        }
    } while (!sc.nextLine().equals("bye"));
}   

}

1

There are 1 best solutions below

1
On

You should take your "Bye" out of the loop. So if sc.nextLine().equals("bye") it finishes the loop, say "bye" and finishes the program.

    Scanner sc = new Scanner (System.in);
    String question;

    System.out.println("Hello");

    do
    {
        question = sc.nextLine();

        if (question.equals("how are you");
        {
            System.out.println("good");
        }

    } while (!question.equals("bye"));    

            System.out.println("bye");