Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cleaning the buffer of system.in

Posted on 2006-11-09
4
Medium Priority
?
689 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 1600 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 400 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

721 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