Solved

Cleaning the buffer of system.in

Posted on 2006-11-09
4
683 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

623 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