1

Please explain the use of System.in.read() method in this example that I'm learning about from another post. As a beginner, I find it unclear when I input a number and get different one in the output, please clarify.

import java.io.IOException;

public class MainClass {

public static void main(String[] args) {
    int inChar;
    System.out.println("Enter a Character:");
    try {
        inChar = System.in.read();
        System.out.print("You entered ");
        System.out.println(inChar);
    }
    catch (IOException e){
        System.out.println("Error reading from user");
    }
}
learner101
  • 37
  • 3
  • please look at the official doc for this matter first – Minjun Yu May 23 '18 at 02:26
  • 1
    `read()` reads binary. You're reading the ASCII values. https://www.asciitable.com/ – markspace May 23 '18 at 02:29
  • *Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255.* – Scary Wombat May 23 '18 at 02:30
  • While I agree about the documentation, the documentation isn't supposed to be pedagogical. It doesn't teach you basic things you should already know. This is stuff you should learn in school. *(Edit: note OP's comment about "documentation not clear" was removed.)* – markspace May 23 '18 at 02:32
  • really - what part did you not understand? – Scary Wombat May 23 '18 at 02:32
  • It is exactly **Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.** – learner101 May 23 '18 at 02:33
  • Yup, one byte *of data*. Binary. Doesn't matter if it's characters, JPG image or what, it's all binary under the hood. There has to be some way to read the binary so the data can be interpreted by the code as whatever it's supposed to be. – markspace May 23 '18 at 02:34
  • Have a look at https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html, the docs are clearly mentioning which data type is returned. If you want to read numbers or strings from `System.in`, have a look at `java.util.Scanner` or BufferedReader. – sn42 May 23 '18 at 02:35
  • wonderful, appreciate your comments and help – learner101 May 23 '18 at 02:36

1 Answers1

0

System.in.read() reads values as their binary value; for example reading "a" will result in the value of "97" - the mapping of this is available here https://www.asciitable.com/.

In order to get the textual representation of this in Java you want to read this as either a Character or a String - Character is a single value, while a String is a combination of Characters one after the other. For example:

public class MainClass {

public static void main(String[] args) {
        System.out.println("Enter a Character:");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        System.out.print("You entered ");
        System.out.println(input);
}

Have a look at the Scanner class to see other options, you can use scanner.nextInt() to get an Integer back.