I am new to Java and this is part of my chatbot code. When I run createQuestions()
from showMenu()
it doesn't seem to work. What createQuestion()
does is let the user create questions and chat with themselves.
The problem is here
Welcome!
Choose your option:
1) Add Questions
2) Chat(You need to add question first)
3) Know more about Towns
4) Exit
You: 1
Creating questions...Type 'end' if you wish to stop Question? (it doesn't pause here as I need to read what the user input and store it as question)
You:
How many responses do you want? :
//Start of ShowMenu():
txtChat.append("\nWelcome!\nChoose your option:");
txtChat.append("\n1)Add Questions\n2)Chat(You need to add question first)\n3)Know more about Towns\n4)Exit\n");
txtChat.append(">>>\n");
txtEnter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showMenu();
}//end actionPerformed
});//end actionListener
}//end TestinChatBot
public void showMenu() {
String choice;
do {
switch (choice) {
case "1":
createQuestions();
break;
case "2":
startChat();
break;
case "3":
knowtowns();
break;
case "4":
txtChat.append("\nFinally! I can play MapleStory! Sayonara!");
System.exit(0);
break;
default:
break;
}
} while (!choice.equals("4"));
}
public void createQuestions() {
txtChat.append("\nCreating questions...Type 'end' if you wish to stop\n");
do {
txtChat.append("Question? \n");
q = txtEnter.getText();
txtChat.append("You: " + q + "\n");
if (!q.contains("end")) {
txtChat.append("How many responses do you want? : ");
noOfResponses = Integer.parseInt(txtEnter.getText());
txtEnter.setText("");
String r[] = new String[noOfResponses];
if (noOfResponses > 0) {
for (int i = 0; i < noOfResponses; i++) {
txtChat.append("Response " + (i + 1) + ": ");
r[i] = txtEnter.getText();
txtEnter.setText("");
}
Chat newChat = new Chat(q, r);
addQuestion(newChat);
txtChat.append("\n" + Arrays.toString(r));
} else {
txtChat.append("Please enter a number bigger than 0");
}
} else {
showMenu();
}
} while (q.equalsIgnoreCase("end") == false);
}
and the errors are these
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
If txtEnter.getText()
returns blank you get:
java.lang.NumberFormatException: For input string: ""
because blank can not be converted to a number.
So you should check to make sure that txtEnter.getText()
does not return blank.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.