I am new to Java and trying to make a simple mini chatbot, that answers to 3 questions with user input and if/else statement.
I do not know if I am correct, but I got this far:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Hello there! It's Chatbot here. How can I help you?");
boolean running = true;
while (running == true) {
System.out.println(" ");
String currentLine = input.nextLine();
if (input.equals("Hi")) {
System.out.println("Hi there! Any way I can help you?");
} else if (input.equals("What do you eat?")) {
System.out.println("I eat only your battery and internet!");
} else if (input.equals("Who made you?")) {
System.out.println("I was made thanks to Reinis!");
} else if (input.equals("Bye")) {
System.out.println("Bye! Have a nice day!");
} else {
System.out.println("Sorry! I am new to this and I didn't understand you. Try to ask me in a different way!");
}
}
}
}
When I run it everything goes to if statement, can you point at the right direction where I am wrong? And maybe what is a better way to create this. Does java has word containing options from user input? I am not asking for code, just for pointers, thanks!
The problem is that you are using the same variable input
as String and as Scanner, change the name of the variable like this:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Hello there! It's Chatbot here. How can I help you?");
boolean running = true;
while (running == true) {
System.out.println(" ");
String currentLine = input.nextLine();
switch(currentLine) {
case 'Hi':
System.out.println("Hi there! Any way I can help you?");
break;
case 'What do you eat?':
System.out.println("I eat only your battery and internet!");
break;
case 'Who made you?':
System.out.println("I was made thanks to Reinis!");
break;
case 'Bye':
System.out.println("Bye! Have a nice day!");
break;
default:
System.out.println("Sorry! I am new to this and didn't understand. Try to ask me in a different way!");
break;
}
}
}
}