java - Flush/Clear System.in (stdin) before reading -


at work, have 5 rfid readers attached pc running linux. readers recognized keyboards , send input (what read form chip) key-input-event sequence. able tell reader send sequence, i'm doing raw-read on /dev/input/xx , input way.

the problem is, send keyboard-events generated rfid readers still "in" stdin , when try read system.in via scanner (input should generated normal keyboard time), first "pending" input readers (which consists of 10 hex-decimal digits , newline (\n)).

now, question is: how can flush these "pending" input's stdin , read want keyboard?

i tried:

system.in.skip(system.in.available()); 

but seek not allowed on stdin (skip throws ioexception).

for (int = 0; < system.in.available(); i++){   system.in.read(); } 

but available() doesn't estimate enough (still stuff in stdin afterwards).

scanner scanner = new scanner(system.in); while (scanner.hasnextline()){   scanner.nextline(); } system.out.println("clean!"); 

but hasnextline() never becomes false (the print never executes).

bufferedreader in = new bufferedreader(new inputstreamreader(system.in)); string line; while ((line = in.readline()) != null); system.out.println("clean!"); 

same above.

anyone more ideas?

based on @joni's advice, put together:

scanner scanner = new scanner(system.in); int choice = 0; while (scanner.hasnext()){     if (scanner.hasnextint()){         choice = scanner.nextint();         break;     } else {         scanner.next(); // discard this, not interested...     } } 

this discards data "pending" in stdin , waits until valid data entered. valid, in context, meaning decimal integer.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -