Aaron Janke
Aaron Janke

Reputation: 47

allowing multiple user inputs in java

I am making an A.I that carries on a conversation with you. when you run the program, the computer says hi, and the user can enter multiple greetings back (like you, howdy etc.)

my question is when the computer asks the user "How are you?" and the user answers. I programmed a switch where if you say something like "good" the computer will reply "Glad to hear it." , but it doesn't. what did I Do wrong?

here is my code:

    System.out.println("hi");
    Thread.sleep(3000);

    System.out.println("The computer would like to remind you to reply with only a greeting");

    Scanner rexy = new Scanner(System.in);
    String domino = rexy.nextLine();


    switch (domino){
        case "hello":
            System.out.println("How are you?");
            break;

        case "hi":
            System.out.println("How are you?");
            break;

        case "howdy":
            System.out.println("How are you?");
            break;

        case "heyo":
            System.out.println("How are you?");
            break;

        case "hello?":
            System.out.println("How are you?");
            break;

        case "hey":
            System.out.println("How are you?");
            break;

        case "sup":
            System.out.println("How are you?");
            break;

        case "good":
            System.out.println("Glad to hear it");
            break;

        case "great":
            System.out.println("Glad to hear it");
            break;

        case "awesome":
            System.out.println("Glad to hear it");
            break;

        case "splendid":
            System.out.println("Glad to hear it");
            break;

        case "fantastic":
            System.out.println("Glad to hear it");
            break;


        case "fine":
            System.out.println("Glad to hear it");
            break;

        case "what's crackalakin?":
            System.out.println("How are you?");
            break;

        case "what's up turd face?":
            System.out.println("That's rude! How are you?");
            break;

    }
}

}

thanks.

Upvotes: 0

Views: 181

Answers (2)

Michał Schielmann
Michał Schielmann

Reputation: 1382

If you really would like to do this simple but not really elegant - you can put the readLine and app answers in some kind of loop like below:

public class Test {

    public static void main(String args[]) throws InterruptedException {
        System.out.println("hi");
        Thread.sleep(3000);

        System.out.println("The computer would like to remind you to reply with only a greeting");

        Scanner rexy = new Scanner(System.in);

        boolean saidBye = false;

        while (saidBye == false) {
            String domino = rexy.nextLine();
            switch (domino) {
                case "hello":
                case "hi":
                case "howdy":
                case "heyo":
                case "hello?":
                case "hey":
                case "sup":
                case "what's crackalakin?":
                    System.out.println("How are you?");
                    break;
                case "good":
                case "great":
                case "awesome":
                case "splendid":
                case "fantastic":
                case "fine":
                    System.out.println("Glad to hear it");
                    break;
                case "what's up turd face?":
                    System.out.println("That's rude! How are you?");
                    break;
                case "bye":
                    System.out.println("Bye bye");
                    saidBye = true;
                    break;
            }
        }
    }
}

More elegant way would be to put every part of the conversation into separate method like that:

public class Android {

    private Scanner rexy;

    public static void main(String args[]) throws InterruptedException {

        Scanner rexy = new Scanner(System.in);
        Android android = new Android(rexy);
        android.greet();
        android.beNice();
        android.sayBye();
    }

    public Android(final Scanner rexy) {
        this.rexy = rexy;
    }

    public void greet() {

        System.out.println("hi");

        System.out.println("The computer would like to remind you to reply with only a greeting");

        String domino = rexy.nextLine();
        switch (domino) {
            case "hello":
            case "hi":
            case "howdy":
            case "heyo":
            case "hello?":
            case "hey":
            case "sup":
            case "what's crackalakin?":
                break;
            default:
                System.out.print("That's rude! ");
                break;
        }
    }

    private void beNice() {
        System.out.println("How are you?");
        String domino = rexy.nextLine();

        switch (domino) {
            case "good":
            case "great":
            case "awesome":
            case "splendid":
            case "fantastic":
            case "fine":
                System.out.println("Glad to hear it");
                break;
        }
    }

    private void sayBye() {
        System.out.println("OK. I have to go now.");
        String domino = rexy.nextLine();

        switch (domino) {
            case "bye":
            case "cu":
            case "see you":
                System.out.println("Bye");
                break;
        }
    } 
}

But it still has a lot of room for improvement ;)

Upvotes: 0

Luan Nico
Luan Nico

Reputation: 5917

You could try adding a default statement to the switch, so that there is fallback for when the answer is not recognized; like that:

switch (domino) {
  //...
default:
    System.out.println("Sorry, I don't understand that.");
    break;
}

Furthermore, you can try printing the domino String, to see what is actually being read.

System.out.println(domino);

Also, a tip: you can join multiple equal case statements in a switch like so:

switch (domino) {
    case "hello":
    case "hi":
    case "howdy":
    case "heyo":
    case "hello?":
    case "hey":
    case "sup":
        System.out.println("How are you?");
        break;
    case "good":
    case "great":
    case "awesome":
    case "splendid":
    case "fantastic":
    case "fine":
        System.out.println("Glad to hear it");
        break;
}

Upvotes: 2

Related Questions