0

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.

8
  • 8
    @TNT, it works since java 7. Good morning :) Commented Aug 18, 2014 at 22:24
  • 2
    What errors do you get? Why doesn't it work? Does it gives a compilation error, runtime error, or simply prints nothing? Commented Aug 18, 2014 at 22:26
  • 1
    When something in code isn't working, please specify HOW it isn't working. Is it giving the wrong result? Is it throwing an exception? Commented Aug 18, 2014 at 22:26
  • 1
    Not really related to your question, but you should read about how switch statements default to fall through. You really don't need the same println() statement over and over. Commented Aug 18, 2014 at 22:31
  • 1
    Although not really related to your question, I'd suggest using rexy.nextLine().toLowerCase() or switch(domino.toLowerCase()) so that the domino String can match up with any of the case statements regardless of case. Commented Aug 18, 2014 at 22:48

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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 ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.