What Is the Use of System.In.Read()

What is the use of System.in.read()?

May be this example will help you.

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");
}
}
}

What is System.in.read()

System.in returns an InputStream. The method read() without parameters "Reads the next byte of data from the input stream." according to the documentation.

Use System.in.read() repeatedly

Here's another one that matches original code intent but provides a Scanner and while loop until there is a match

import java.util.Scanner;

public class GuessTheLetterGame {

public static void main(String[] args) throws java.io.IOException{
Scanner keyboard = new Scanner(System.in);
char answer;
char guess;
boolean right = false;

while(!right){

System.out.print("press a key and press ENTER:");
answer= (char) keyboard.next().charAt(0);

System.out.print("Have a guess and press ENTER: ");
guess= (char) keyboard.next().charAt(0);
if (guess == answer){
System.out.println("**Right**");
right = true;
}
}
}
}

System.in.read() behaviour i can't explain

ENTER on Windows generates 2 characters (CRLF) whereas read() only consumes 1 of them.
You must consume 2 characters for the desired behaviour. Just add another System.in.read() and you will see.

The following explains the generation and consumption of characters when you press ENTER. 13 represents CR and 10 represents LF.

F
13i10r
13s10t
13 10S
13t10r
13i10n
13g10

What does System.in.read actually return?

49 is the ASCII value of the char 1. It is the value of the first byte.

The stream of bytes that is produced when you enter 10Enter on your console or terminal contains the three bytes {49,48,10} (on my Mac, may end with 10,12 or 12 instead of 10, depending on your System).

So the output of the simple snippet

int b = System.in.read();
while (b != -1) {
System.out.println(b);
b = System.in.read();
}

after entering a 10 and hitting enter, is (on my machine)

49
48
10

System.in.read(); comparing

I made a few changes to your program to give you some insigth into what is happening:

 public static void main(String args[]) throws java.io.IOException {

char ch, ch2;

System.out.print("Hit a key and press Enter: ");

ch = (char) System.in.read();

System.out.println("Hit a second key and press Enter: ");

ch2 = (char) System.in.read();
int x1=ch;
int x2 = ch2;
System.out.println("x1="+x1+" | x2="+x2);
if (ch == ch2) System.out.println("Same"); else System.out.println("different");
}

Run the above program two times :
- type "a" and hit enter.
- hit enter twice

Explication :
System.in is an instance Of InputStream and the read method of an InputStream https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html that "Reads the next byte of data from the input stream".
The problem is that when you re hitting enter you are adding an extra byte to the input stream and ALWAYS ch2 will store it.
*Note : x1, x2 will display the ASCII codes of the chars you hit.

Reading in from System.in - Java

You can use System.in to read from the standard input. It works just like entering it from a keyboard. The OS handles going from file to standard input.

import java.util.Scanner;
class MyProg {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Printing the file passed in:");
while(sc.hasNextLine()) System.out.println(sc.nextLine());
}
}


Related Topics



Leave a reply



Submit