Solved

Cleaning the buffer of system.in

Posted on 2006-11-09
4
679 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using executorService 3 25
Selenium findElement(By.classname  identifier 15 38
Firefox and firebub 5 29
Overriding a method 3 15
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

710 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