0

This is a code from a Decision Support System I'm trying to make. Bascially the question is whether you want breakfast or not. StdIn.readChar() is used to read a char you insert in the console. I am 100% sure it's correct.

Obiously there is a main method missing, I believe it is clear to understand without it, but if you need it I can paste it in later.

Now my Question:

If I run this programm I am asked in the main whether I am hungry or not. So I type j (in german 'ja' is 'yes') and the programm jumps into the Breakfast method. But then I am asked if I want breakfast but there is no time to insert a char. It directly jumps into the Default method. I have used the print command to see on wich point, as you can see. Did I do something wrong with the switch-case selection?

My console says this:

Are you hungry?

(I typed) j

Do you want breakfast?

2 (this is due to the testing println command)

This insert is invalid

Do you want breakfast?

And the strange point is, at this moment I am able to write my answer. I have the same problem at the question "Do you like it to be warm?", but of course I can never answer because I need to answer the first question (Do you want breakfast) again.

Can you help me? Do I need to delete the do-while-loop? Am I doing so wrong I need to delete everything? :D

 public static void Breakfast(){
    int a = 0;
    do {
        System.out.println("Do you want Breakfast?");
        char y = StdIn.readChar(); //read Char from console

        switch (y) {
            case 'j':
                System.out.println("Do you like it to be warm?");
                y=StdIn.readChar();

                switch (y) {
                    case 'j':
                        System.out.println("Eggs with Bacon");
                        break;
                    case 'n':
                        System.out.println("Cereal");
                        break;
                    default: 
                        System.out.println("1");
                        a=Default();
                }
                break;
            case 'n':
                Mittagessen();
                break;
            default: 
                System.out.println("2");
                a=Default();

        }
    } while (a!=0);
}

public static int Default (){
    System.out.println("This insert is invalid");
    return 1;
}

1 Answers1

0

Pressing enter actually gives two characters: Does Windows carriage return \r\n consist of two characters or one character?

So, in your case you have first read the J character and on your second question you had already fed in the second character coming from pressing the Enter button.

I hope this helps!

Sofo Gial
  • 671
  • 1
  • 9
  • 20
  • 1
    Oh thank you very much! Do you have an idea how to fix it, besides writing the read-Statement two times? – Sophie Richter Nov 06 '18 at 15:10
  • I am not familiar with the `StdIn` lib you are using. Probably using `Scanner` could help you overcome the problems: https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java – Sofo Gial Nov 06 '18 at 15:24