System.in.read() gets skipped without prompting for input

mcfadizzle :

I wrote a code which is kind of an interview. It asks you how old you are and what your gender is. For some reason the .exe gets terminated after it prints out the second question. Why is that? How can I fix it?

I checked the syntax but can't seem to find the mistake.

import java.io.IOException;

public class Abfrage {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    char Geschlecht;
    System.out.println("Wie ist dein Geschlecht? M oder W?");
    Geschlecht = (char) System.in.read();

    int Alter;
    System.out.println("Wie ist dein Alter?");
    Alter = (int) System.in.read();
    if (Alter >= 18 && Alter <= 100)
    {
        System.out.println("Du bist volljaehrig, herzlichen Glueckwunsch!");
    }
}

}

It prints out this string ("Wie ist dein Alter?") and after that it gets terminated. I can't put in my age(number) which is the question asked in the println.

I expect it to let me put in my age, and if I am between 18 and 100 it should tell me "congrats on being an adult." (its not finished there are more if/else)

Edit: I tried adding an if else statement. When I run the .exe:

    if (Alter >= 18 && Alter <= 100)
    {
        System.out.println("Du bist volljaehrig, herzlichen Glueckwunsch!");
    }
    else if (Alter >= 0 && Alter <= 17);
    {
        System.out.println("Du bist leider minderjaehrig mein Kind");
    }

When I did this, it just prints out the else if println "Du bist leider minderjaehrig mein Kind" after the question "Wie ist dein Alter?". It terminates after that.

Zabuza :

Explanation

You likely entered more than just one character.

You clicked for example M for male but also hit the enter button. Your actual input is

M\n

and not just

M

and the first read reads the M while the second reads the \n without asking you for new input, since there is still some unread input left.

So Alter is the int value of the \n character and thus your if block is not entered. So the code executed fully.


Solution

There is a handy class which was developed exactly for the purpose to easy such input reading, Scanner (documentation).

Scanner scanner = new Scanner(System.in);
System.out.println("Wie ist dein Geschlecht? M oder W?");
String geschlecht = scanner.nextLine();

System.out.println("Wie ist dein Alter?");
int alter = Integer.parseInt(scanner.nextLine());
if (alter >= 18 && alter <= 100) {
    System.out.println("Du bist volljaehrig, herzlichen Glueckwunsch!");
}

Note

Be aware that the Java naming convention says camelCase for variable names, not PascalCase. So your Geschlecht and Alter should rather be called geschlecht and alter.

Also, you should create your variables at the exact position you need them, and not earlier. So instead of

char geschlecht;
System.out.println("Wie ist dein Geschlecht? M oder W?");
geschlecht = ...

prefer

System.out.println("Wie ist dein Geschlecht? M oder W?");
char geschlecht = ...

That will make your code easier to read.

Depending on your operating system, the newline character may not just be \n but can also be \n\r. Very cumbersome to handle this stuff yourself, Scanner will do all of that for you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=85221&siteId=1