Solved

Cleaning the buffer of system.in

Posted on 2006-11-09
4
665 Views
Last Modified: 2008-02-01
public class MyClass {
public static void main(String args[]){

      try{
            System.out.print("Pls enter one character and press enter");
            System.out.println(System.in.read());
            System.out.print("Pls enter another character");
            System.out.println(System.in.read());

      }catch(Exception e){
            
      }
}
}

What I received
Pls enter one character: a<--only key in here, all the result come out, seems like it buffer the enter
97
Pls enter another character: 13


What I expect
Pls enter one character: a<--key in here
97
Pls enter another character: b<--and key in here
98

What I expect
Pls enter one character: <- enter here
13
Pls enter another character: b<--and key in here
98


How to clear the buffer of system.in and read the character again?
0
Comment
Question by:william007
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
owenli27 earned 400 total points
ID: 17909103
If work in DOS, you can use skip() to skip 2 bytes. 1 is line feed and 1 is Carriage return.

try{
        System.out.print("Pls enter one character and press enter");
      System.out.println(System.in.read());
      System.in.skip(2);

      System.out.print("Pls enter another character");
      System.out.println(System.in.read());
      System.in.skip(2);

}catch(Exception e){
 
}
0
 
LVL 9

Expert Comment

by:owenli27
ID: 17909138
System.in.skip(System.in.available());  is better and it can be used for both *nix or win
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 17910002
stdin is buffered, read() only returns after a CR is entered.
0
 
LVL 9

Author Comment

by:william007
ID: 17932839
Thanks:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now