0

I have a procedural Java Program that has multiple segments that I would like to execute manually (using "Press Enter to Proceed").

Right now I've been trying with System.in.read() or Scanner but if I would make multiple enter presses, it automatically executes the next segment.

how might I regulate this behavior?

growclip
  • 95
  • 8
  • possible duplicate:http://stackoverflow.com/questions/18281543/java-using-scanner-enter-key-pressed?rq=1 – thetraveller Jan 23 '17 at 14:43
  • Can you empty the input buffer after each segment has completed? I've not tried it, but does something like [pseudo code] System.in.read([byte array of in.available() length]); to "flush" everything. – Steve Smith Jan 23 '17 at 14:47

2 Answers2

0

I would suggest using BufferedReader instead of System.in.read() . It has a built-in flush mechanism too.

Rajarshi basu
  • 322
  • 1
  • 10
0

You have to remove everything from the InputStream before you wait for the next enter.

System.in.available() returns the number of bytes currently in the stream.

System.in.read(new byte[System.in.available()]);

This clears the Stream, so now the user has to enter something again.

Note: System.in.skip() doesn't work for some reason.

Jacques Gaudin
  • 12,304
  • 3
  • 40
  • 64
Poohl
  • 1,823
  • 1
  • 5
  • 19