0

Why Number lower than 9 is printed twice in my console, in the bellow code example?

public static void main(String[] args) throws java.io.IOException {
    System.out.println("Input from keyboard should be '49'");
    char e;
    for (int a = 0; ; a++) {
        e = (char) System.in.read();
        if (e == 49)
            break;
        else
            System.out.println("Use number lower than 9");
    }
}

And here is the output when you type anything except of number 1:

run:
Input from keyboard should be '49'
2
Number lower than 9
Number lower than 9

Edit

Basicly, I am trying to get an input from console and store it inside char e. What I'm getting after I type 1 is value 49. That's why I used in code above e == 49. I dont know how to retrieve just character 1. Next thing is that i used an infinite loop to force the input to ask for specific character until its the correct one.

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
  • see http://stackoverflow.com/questions/21949775/java-using-the-same-scanner-variables-to-store-multiple-sets-of-entries/21950307#21950307 – Leo Feb 22 '14 at 13:45
  • 6
    You never print "Number lower than 9" in your code... – assylias Feb 22 '14 at 13:46

4 Answers4

2

When you enter 2 in the command line, you actually enter 2\n (with \n being the newline character), as you confirm your input by hitting Enter or Return.

System.in.read() therefore returns a 2 in the first loop cycle and the newline character in the second loop cycle, each time printing your message on the standard output.

You can solve this by skipping newline characters:

for (int a = 0; ; a++) {
    e = (char) System.in.read();
    if (e == '\n')
        continue;
    if (e == 49)
        break;
    else
        System.out.println("Use number lower than 9");
}

There are some more issues though: What are you trying to achieve? if you compare the input on a character-based level, 49 equals not to the number 49, but to the character with the code 49, which is the character '1'.

Secondly, if you are building an endless loop, you should also write it that way. A for loop gives the impression, the loop will be left based on your int a, which is not the case. To write an endless loop, you can just write:

while (true) {
    // do something
    if (checksomething())
        break;
}
Stefan Neubert
  • 1,053
  • 8
  • 22
0
    public static void main(String[] args) throws java.io.IOException {
    System.out.println("Input from keyboard should be '49'");
    char e;
    Scanner s=new Scanner(System.in);
    for (;;) {
        e = s.next().charAt(0);
        if (e == 49){
            System.out.println("Yay");
            break;
        }else{
            System.out.println("Use number lower than 9");
        }
    }
}

You can just scan for the string but if u need chars the code above works

ilgazer
  • 110
  • 9
0

Your program output showing Number lower than 9 because it System.in.read() returns the ASCII value of first character entered that is '2' and its not 49 its 50.

If you want integer as input you can use this code int i = new Scanner(System.in).nextInt();

Refer this Question for more information

Community
  • 1
  • 1
Nithin CV
  • 1,046
  • 9
  • 23
0

read() doesn't read a number, it reads one byte and returns its value as an int. If you enter a digit, you get back 48 + that digit because the digits 0 through 9 have the values 48 through 57 in the ASCII encoding.

source

for (;;) {
    int e = System.in.read(); // char e = (char)System.in.read();
    if (e == 49) // if (e == '1')
        break;
    else
        System.out.println("You entered something else than character 1");
}

Enter is counted as a character, hence another character entry.

If you want to store just character 1, there is no point of casting it.

    if (e == 49) {
       //Presume that is character `1` and do what operation you want as it is.
       System.out.println("You enter number 1");
    }

Instead you should check for correct input and then cast it to character. You can check the input if is number as I wrote in the quote by checking the input if is between 48 and 57. Remember, if you type e.g. 57 is counted as two inputs(5 and 7) .

    char ch; int e;
    if (e >= 49 && e <= 57) {
       ch = (char)e;
       System.out.println("You enter number " + ch);
    }

This all can been done easily with Scanner.nextInt() as others mentioned which casts,and spots easily the next int number, but be careful ,Scanner works differently ,it checks tokens rather than characters, meaning delimiter would be by default an empty space (" "). So if you type 57 (and press Enter) it will cast it to number 57.

The convenience is that if you type dogs 57 (and press Enter) it will still gets as an input 57 and skip dogs. Another convenient method is hasNextInt(), so you can use it inside a while loop to get several number inputs.

You can also change the delimiter useDelimiter(), if you want to get it's character, set it to ("") empty string.

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65