Question: I'm creating a simple chat box. I am recieving an error that is that if the user input is not put in a certain order,
I'm creating a simple chat box. I am recieving an error that is that if the user input is not put in a certain order, the program does not run but just outputs spaces. I believe it has something to do with my Scanner call code like my variables: in+number and resp+number as they change with each new keyword.
Can someone give me a forloop or an idea of how to change my program so that the code will take whatever keyword the user types in and respond regardless of the user's input order? Hope this makes sense. Thx
I think the bolded examples below are the problem:
Scanner in5 = new Scanner(System.in); String resp2 = in5.nextLine(); if (resp2.contains("mom") || resp2.contains("mother")) {
Scanner in6 = new Scanner(System.in); String resp3 = in6.nextLine(); if (resp3.equalsIgnoreCase("I said no!")) {
Ouput:
run: Hello, my name is Chatters! What's your name? Jane Wow, your name is short! What a lovely name, and what is your last name? Doe are you a boy or a girl? girl YAY! Girl Power! Pleased to meet you so... Jane? Wanna ask me a question? :D yes That's more like it! Ask away! do you have a mother? tell me more about your family what weather is it?
//program does not end but it works if I type "I said no!" in place of weather and continues the program.
Here is the full code:
import java.io.*; import java.util.*; import java.lang.*;
public class ChatBox{
public static void main (String[] arg){ System.out.println("Hello, my name is Chatters! What's your name?"); Scanner in = new Scanner(System.in); String name = in.nextLine(); /* need forloop for both resp + number and in + number for(i=0; i>=0; i++); so that user can ask questions as many times as want and in any order */
name.trim(); if (name.length()>15){ System.out.println("Wow, your name is long!"); } if (name.length()<5){ System.out.println("Wow, your name is short!"); }
System.out.println("What a lovely name, and what is your last name?"); Scanner in2 = new Scanner(System.in); String namer2 = in2.nextLine();
namer2.trim(); System.out.println("are you a boy or a girl?"); Scanner in3 = new Scanner(System.in); String gender = in3.nextLine(); char gen = 'f'; if (gender.equalsIgnoreCase("boy") || gender.equalsIgnoreCase("man") || gender.equalsIgnoreCase("guy") || gender.equalsIgnoreCase("m") || gender.equalsIgnoreCase("male") ){ gen = 'm'; System.out.println("Knew it. You seemed to have the messy handwriting of a boy!"); System.out.println("lol! You're a boy! I'm a girl.... I guess you can tell."); //System.out.println("Pleased to meet you"); }else if (gender.equalsIgnoreCase("girl") || gender.equalsIgnoreCase("woman") || gender.equalsIgnoreCase("lady") || gender.equalsIgnoreCase("f") || gender.equalsIgnoreCase("female") ){ gen = 'f'; System.out.println("YAY! Girl Power!"); } else { System.out.println("Your answer is not quite clear..."); System.out.println("That's not a gender silly!"); //System.out.println("Pleased to meet you"); } System.out.println("Pleased to meet you");
System.out.println("so... "+name +"? Wanna ask me a question? :D"); Scanner in4 = new Scanner(System.in); String resp = in4.nextLine(); //String resp = in.nextLine(); if (resp.equalsIgnoreCase("No") || resp.equalsIgnoreCase("Nope") || resp.equalsIgnoreCase("n") || resp.equalsIgnoreCase("Not")){ System.out.println("Then scram already! :CCCCC"); System.exit(0); } //EXIT PROGRAM //if response is not a question (does not contain ?) and contains yes, etc. in it, then output: if ((resp.equalsIgnoreCase("yes") || resp.equalsIgnoreCase("ok") || resp.equalsIgnoreCase("y") || resp.equalsIgnoreCase("sure"))){ System.out.println("That's more like it! Ask away!");
Scanner in5 = new Scanner(System.in); String resp2 = in5.nextLine(); if (resp2.contains("mom") || resp2.contains("mother")) { System.out.println("tell me more about your family"); } Scanner in6 = new Scanner(System.in); String resp3 = in6.nextLine(); if (resp3.equalsIgnoreCase("I said no!")) { System.out.println("why so negative?"); } Scanner in7 = new Scanner(System.in); String resp4 = in7.nextLine(); if (resp4.contains("weather") || resp.contains("Weather")) { System.out.println("you don't say."); } Scanner in8 = new Scanner(System.in); String resp5 = in8.nextLine(); if (resp5.contains("know" + "brother")) { System.out.println("Hey! That reminds me of my brother...he's just like me... except he's a microwave."); } Scanner in9 = new Scanner(System.in); String resp6 = in9.nextLine(); if (resp6.contains("dog") || resp6.contains("cat")) { System.out.println("Tell me more about your pets!"); } Scanner in10 = new Scanner(System.in); String resp7 = in10.nextLine(); if(resp7.contains("Mr. Binz") || resp7.contains("Binz")) { System.out.println("He sound like an excellent teacher!"); } //add 2 more noncommital responses to possible random responses Scanner in11 = new Scanner(System.in); String resp8 = in11.nextLine(); if(resp8.contains("family")) { System.out.println("oooh. Tell me more about them. Any good family stories?"); } Scanner in12 = new Scanner(System.in); String resp9 = in12.nextLine(); if(resp9.contains("video games")) { System.out.println("I LOVE video games. Have you played any good ones?"); } //end to noncommital responses to possible random responses //adding trim to respond to nothing questions /* Scanner in13 = new Scanner(System.in); String resp10 = in13.nextLine(); trim.in13(); //supposed to trim out spaces if(resp10.contains(in13.length = 0)) { //supposed to find length of 0 if user inputs nothing because trim took out spaces System.out.println("Say something please."); } */ else { System.out.println("nice chatting with you! G'bye! :DD");
// resp = getRandomResponse(); } //return resp; } } /** * Pick a default response to use if nothing else fits. * @return a non-committal string */ private String getRandomResponse() //Use this for when user asks a question/says something other than the kywords typed above { final int NUMBER_OF_RESPONSES = 4; double r = Math.random(); int whichResponse = (int)(r * NUMBER_OF_RESPONSES); String response = ""; if (whichResponse == 0) { response = "Interesting, tell me more."; } else if (whichResponse == 1) { response = "Hmmm."; } else if (whichResponse == 2) { response = "Do you really think so?"; } else if (whichResponse == 3) { response = "You don't say."; }
return response; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
