4
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.

alexrnov
  • 2,346
  • 3
  • 18
  • 34
  • 4
    What operating system are you running on? If you're running on Windows, then enter is carriage-return line-feed, i.e "\r\n" - two characters. – Jon Skeet Apr 06 '17 at 12:15
  • 1
    @JonSkeet it works fine on window , just tested – Pavneet_Singh Apr 06 '17 at 12:16
  • in eclipse if you add several characters after `F` and then press enter, the next line has as many characters as previous. example: `F11111111111111111` then enter, the output will be the rest of the string – XtremeBaumer Apr 06 '17 at 12:17
  • 1
    @JonSkeet I checked on Linux Mint it works just fine. It goes letter by letter after `enter` pressed. – catch23 Apr 06 '17 at 12:18
  • @user3260381 make sure you are not entering `space` and then `enter` button – Bhushan Uniyal Apr 06 '17 at 12:20
  • i am only pressing enter key, and with each press i am getting 2 characters on one line. –  Apr 06 '17 at 12:22
  • @Pavneet_Singh How did you test this on Windows? I can reproduce this behavior in Eclipse. – Pshemo Apr 06 '17 at 12:22
  • @Pshemo i just reproduced this in eclipse mars .2 , and i found that the next line will print more characters if i add more spaces – Donat Pants Apr 06 '17 at 12:24
  • 1
    @user3260381 That is because when you press enter, like Jon already pointed out in Windows you are generating two characters: carriage-return `\r` and line-feed `\n` which allows `System.in.read()` execute twice. – Pshemo Apr 06 '17 at 12:24
  • @user3260381 `javac` is for compilation only. Are you run this program from an IDE or with `java` command? – catch23 Apr 06 '17 at 12:25
  • @Pshemo and thats the reason it gets skipped once? – XtremeBaumer Apr 06 '17 at 12:26
  • 1
    @XtremeBaumer What makes you think it is skipped? At start of loop we are printing first character, then waiting for user input which is enter key, so console is moving cursor to next line but in standard input we have two characters, so now `System.in.read()` will read twice which allows two iterations (two characters in single line). When there will be no more characters process waits for user input and when user again press enter we are moved to next line and standard input has again `\r\n` to read. – Pshemo Apr 06 '17 at 12:30
  • @Pshemo how does that explain the face that when I input `n` spaces and then press enter it will print `n+1` characters? – Donat Pants Apr 06 '17 at 12:35
  • @DonatPants on Windows if you put `n` spaces it would print `n+2` characters (+2 because of `\r\n` in standard input). `System.int.read()` consumes one character at a time which happens once in each iteration. Loop will pause when there will be no more characters to read. – Pshemo Apr 06 '17 at 12:41
  • @JonSkeet @Pshemo when I use `int j = System.in.read` and `System.out.println(j)` inside the `for` loop the output is 13 and 10, and when i delete the `for` loop the output is only 10 suggesting each press of enter key is causing the loop to execute twice –  Apr 06 '17 at 12:42
  • @pshemo you are right, i should have said it will print `n+1` ADDITIONAL charecters, because the first charecter printed is to be expected. – Donat Pants Apr 06 '17 at 12:44
  • @Pshemo i tested on netbeans, win 7 – Pavneet_Singh Apr 07 '17 at 03:57

4 Answers4

4

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:

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

Pshemo
  • 122,468
  • 25
  • 185
  • 269
1

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
        }
    }
}
Donat Pants
  • 379
  • 4
  • 11
1

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;
Zubair Nabi
  • 1,016
  • 7
  • 28
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

VivekRatanSinha
  • 596
  • 1
  • 4
  • 17
  • Your answer fundamentally explained what I was missing in my code, cleared the concept around my query and sleekly made it work. –  Apr 06 '17 at 17:03
  • 1
    @user3260381 While adding another `System.in.read()` may be valid solution in your specific case it is worth mentioning that it will work only in Windows because other OS use single line separators. Another potential problem is that `System.in.read()` will wait until user will press enter key, but it will not prevent user from typing other characters before pressing enter which will also put into standard input (`System.in` from which you try to read characters) more than two characters which can cause additional loops. To solve it it is better to consume entire line, not just line separators. – Pshemo Apr 06 '17 at 18:13