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

2019-02-25 01:13发布

问题:

class E92StringDemo {

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

      String strObj1 = "First String";

      for(int i=0;i<strObj1.length();i++) {

        System.out.print(strObj1.charAt(i));
        System.in.read(); //just to pause the execution till i press enter key

      }
   }
}

I want the output to come like:

F
i
r
s
t...

but the output is coming like:

F
ir
st
S
tr
in
g

I am not sure how come 2 characters are getting displayed in one line with every press of an enter key(\n)?

I am running windows 8 and using a command prompt to run the file using javac.

回答1:

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



回答2:

Problem

System.in.read() only holds execution of your application if there is no data to read in standard input stream (represented by System.in).

But in console when you press ENTER, two things happen:

  • console cursor is moved to next line
  • OS dependent line separator* is placed in standard input, which for Windows is \r\n:

    • carriage return \r - placed at index 13 in Unicode Table
    • line feed \n - placed at index 10 in Unicode Table

So as you see if you want to pause your loop in each next iteration, you will need to empty data from input stream before leaving current iteration. But System.in.read() reads only one character at a time, in your case \r leaving \n for next iteration (so no pause there).

So before pause will be again available you need to read twice in one iteration.

Solution

If you want to get rid of this problem in OS independent way use BufferedReader#readLine() or Scanner#nextLine like:

String strObj1 = "First String";
try(Scanner sc = new Scanner(System.in)){//will automatically close resource
    for (int i = 0; i < strObj1.length(); i++) {
        System.out.print(strObj1.charAt(i));
        sc.nextLine();
    }
}

These methods also solve problem of potential extra characters placed before pressing enter, since each of them will also be placed in standard input stream, which would require additional .read() calls.


* along with rest of potential characters which ware provided before pressing enter



回答3:

This will fix the problem you are having, but i cannot explain why you are getting this strange behavior with System.in.read().

class E92StringDemo {
    public static Scanner reader = new Scanner(System.in);

    public static void main(String[] args) {

        String strObj1 = "First String";

        for(int i = 0; i < strObj1.length(); i++) {
            System.out.print(strObj1.charAt(i));
            reader.nextLine(); //just to pause the execution till i press enter key
        }
    }
}


回答4:

use

  new Scanner(System.in).nextLine();

instead of

 System.in.read();

Also you are getting this result using System.in.read because It returns an int besides all the possible values of a byte, it also needs to be able to return an extra value to indicate end-of-stream. So, it has to return a type which can express more values than a byte can.

However as per its Doc.

/**
 * Reads the next byte of data from the input stream. The value byte is
 * returned as an <code>int</code> in the range <code>0</code> to
 * <code>255</code>. If no byte is available because the end of the stream
 * has been reached, the value <code>-1</code> is returned. This method
 * blocks until input data is available, the end of the stream is detected,
 * or an exception is thrown.
 *
 * <p> A subclass must provide an implementation of this method.
 *
 * @return     the next byte of data, or <code>-1</code> if the end of the
 *             stream is reached.
 * @exception  IOException  if an I/O error occurs.
 */

public abstract int read() throws IOException;