0
import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException {
        int number = 0;
        int keyValue = System.in.read();
        keyValue = keyValue - 48;
        number = number * 10 + keyValue;
        System.out.println(number);

        while(true) {
            keyValue = System.in.read();
            keyValue = keyValue - 48;
            number = number * 10 + keyValue;
            System.out.println(number);
            if(keyValue == 120) {
                number = number - 120;
                System.out.println(number);
                break;
            }
        }

        }

    }

I want to get integer value from the keyboard. subtracting 48 is gonna make ASCII Code to be a value that I entered on a keyboard.

I don't only want to do this in first digit, but make a integer whatever I enter on a keyboard, using while loop and if condition.

What do you think is the problem? please help me.

3
  • "subtracting 48" subtracting '0' is clearer, and equivalent. Commented Mar 12, 2019 at 8:33
  • 1
    the problem is you are vague about what your problem is. also, you risk an endless loop with your while(true). if ( keyvalue == 120 ) never evaluates to true, well ... Commented Mar 12, 2019 at 8:33
  • System.in buffers input. System.in.read() won't return anything until it is flushed, e.g. by pressing enter or closing the stream. Commented Mar 12, 2019 at 8:34

3 Answers 3

1

Your code had the break condition too late: number already messed up, multiplied by 10, added the 120 - 48. Theoriginal key value - 48 == 120`.

But I think you wanted to test on the letter 'x'.

    while (true) {
        keyValue = System.in.read();
        if (keyValue == 'x') {
            System.out.println(number);
            break;
        }
        keyValue = keyValue - '0';
        number = number * 10 + keyValue;
        System.out.println(number);
    }

However the console System.in is line buffered, you will not get input before Enter was pressed.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't know if that´s what you are looking for, but if you are trying to create a ASCII to int by substracting the 48 value here is the correct code:

    public static void main(String[] args) throws IOException {
    Scanner sc = new Scanner(System.in);
    int number = 0;
    System.out.println("Please enter your ASCII code!");
    int keyValue = sc.nextInt();
    keyValue = keyValue - 48;
    number = number * 10 + keyValue;
    System.out.println(number);

    while (true) {
        number = 0;
        System.out.println("Please enter your ASCII code!");
        keyValue = sc.nextInt();
        keyValue = keyValue - 48;
        number = number * 10 + keyValue;
        System.out.println(number);
        if (keyValue == 120) {
            number = number - 120;
            System.out.println(number);
            break;
        }
    }
}

Cases:

When you enter 48 you get 0, with 49 you get 1 ... when you enter 168 the loop brokes and the program ends.

I hope that's helpfull for you.

Comments

0

Do you want it to be printed one after other or in a single line. If you want it to be in a single line . Hope you get help from the code snippet below :-

public class Main {

    public static void main(String[] args) throws IOException {
        int number = 0;
        int keyValue = 0;
        /*int keyValue = System.in.read();
        keyValue = keyValue - 48;
        number = number * 10 + keyValue;
        System.out.println(number);*/

        while (true) {
            number =0;
            keyValue = System.in.read();
            keyValue = keyValue - 48;
            if(keyValue == -38){
                System.out.println();
            }else {
                if (keyValue < 0 || keyValue > 122) {
                    System.out.println(" BREAK "+keyValue);
                    break;
                }
                number = number * 10 + keyValue;
                System.out.print(number);
            }
        }

    }

}

Output :-

1
1
2
2
3
3
123
123
345
345
5556
5556
789
789

Comments

Your Answer

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