1

I'm using this code to output text and have the user press enter to show the next line of dialog. However, one press of Enter shows two lines of text. Why is this?

public void pressEnterToContinue() {
    try {
        System.in.read();
    } catch(Exception e) { }
}

For reference, the method is called like this:

pressEnterToContinue();
System.out.println("This is a line of text.");
pressEnterToContinue();
System.out.println("So is this.");
pressEnterToContinue();
System.out.println("And this.");
//etc.

The first line displays alone "This is a line of text." The method waits until the user presses enter, then it displays the next two lines ("So is this." and "And this.") when it should display only one.

I did try implementing a short delay but that didn't solve the issue.

H. Pauwelyn
  • 11,346
  • 26
  • 71
  • 115
4oursword
  • 75
  • 1
  • 8

2 Answers2

2

Pressing the enter key echoes back as a new line. println introduces yet another line. As you type at a console, the O.S. echoes back each and every character, and as such, your input is echoed back and then your code prints it again on the screen. This is so because you are dealing with a shell which is itself a program that pick up your keystrokes and echoes them back.

A shell could be written that does not echo back the user input but would be impractical for fallible humans. You might want to experiment telneting to a smtp server or http server on their respective port and type the appropriate commands to send mail or get a html page back. No echo there. It feels weird.

Tarik
  • 9,314
  • 1
  • 19
  • 35
0

System.in.read() reads one byte. My guess is you are running your code on a platform where a newline is represented by a two byte CR LF sequence, so pressing the Enter key satisfies the first System.in.read() call with the CR, and the second System.in.read() call with the LF. Changing your code to use System.console().readline() instead should fix this, but do check out the discussion and other approaches described here, and the solutions discussed here as well.

And here's a link to info about how newline is represented on a variety of platforms.

Community
  • 1
  • 1
Richard Schwartz
  • 13,485
  • 2
  • 21
  • 39
  • It gives me the error "The method readLine() is undefined for the method InputStream. The suggested fixes are "Change to read(..)" (we know that's not the plan) and "add Cast to System,in", making the line try{((BufferedReader) System.in).readLine(); That gives me the new error "Cannot cast from InputStream to BufferedReader" – 4oursword Apr 26 '15 at 02:36
  • Ah... Yeah, forgot about that. System.console().readline() works, but see the answers here for further discussion and various approaches: http://stackoverflow.com/questions/4644415/java-how-to-get-input-from-system-console I've updated my answer to reflect this. – Richard Schwartz Apr 26 '15 at 03:47
  • System.console().readline() displays all lines of the text at once, as soon as the method is called. – 4oursword Apr 26 '15 at 13:42
  • If the echo issue mentioend by @edTarik is correct (which didn't seem right to me, but could be...) then try changing your system.out.println() calls to system.out.print(). If it works, then we've both learned something from this. – Richard Schwartz Apr 26 '15 at 15:55
  • That didn't work. Using System.console().readLine with system.out.println() prints all lines at once, System.console().readLine with system.out.print() prints all of the "lines" of dialog at once on one line. System.in.read() with system.out.println() prints two lines at a time with correct line breaks, and System.in.read() with system.out.print() prints two lines of dialog at once per line. – 4oursword Apr 26 '15 at 16:56
  • Okay. More searching... Here's another Stackoverflow question that addresses the issue. The answers recommend either the Scanner class or using read() to get two bytes. The latter approach, though, will not be portable to an OS that uses a single byte newline. http://stackoverflow.com/questions/26184409/java-console-prompt-for-enter-input-before-moving-on – Richard Schwartz Apr 26 '15 at 17:31