Press enter to continue oddities (Java) -
i'm using code output text , have user press enter show next line of dialog. however, 1 press of enter shows 2 lines of text. why this?
public void pressentertocontinue() { try { system.in.read(); } catch(exception e) { } }
for reference, method called this:
pressentertocontinue(); system.out.println("this line of text."); pressentertocontinue(); system.out.println("so this."); pressentertocontinue(); system.out.println("and this."); //etc.
the first line displays alone "this line of text." method waits until user presses enter, displays next 2 lines ("so this." , "and this.") when should display one.
i did try implementing short delay didn't solve issue.
system.in.read()
reads 1 byte. guess running code on platform newline represented 2 byte cr lf sequence, pressing enter key satisfies first system.in.read()
call cr, , second system.in.read(
) call lf. changing code use system.console().readline()
instead should fix this, check out discussion , other approaches described here, , solutions discussed here well.
and here's link info how newline represented on variety of platforms.
Comments
Post a Comment