VictorGram
VictorGram

Reputation: 2661

How to resolve? java console application:System.in.readLine issue:User can not see what he is typing

I am writing a simple java console application where program will prompt the user to type something in and then the program control will wait for the user input and do something with that input.

Issue is : while typing on the console , the user can not see what he is typing. What should I do differently so the user can see what he is typing?(user can see what he has typed after he hits 'enter').

I created a java standalone project and built and ran the project using netbean's ant command from command line:

ant run

Below is the whole code that I have.OS is Windows 7. jdk version: 1.6

Below is the code I am using to get user-input from console.

import java.io.*;

public class StatLibraryTest {


 public static void main(String[] args) {
    String CurLine = ""; // Line read from standard in


   try{

          while (!(CurLine.equals("quit"))){
              System.out.println("Enter: " );
              CurLine = readLine();

              if (!(CurLine.equals("quit"))){
                  System.out.println("You typed: " + CurLine);
              }
          }
       }catch(Exception e){
            System.out.println(e.getMessage());
       }

  }


    public static String readLine()
    {
        String s = "";
        try {
            InputStreamReader converter = new InputStreamReader(System.in);
            BufferedReader in = new BufferedReader(converter);
            s = in.readLine();
        } catch (Exception e) {
            System.out.println("Error! Exception: "+e); 
        }
        return s;
}
}

Upvotes: 0

Views: 859

Answers (1)

Stephen C
Stephen C

Reputation: 719551

Character echoing is normal taken care of by the operating system's terminal driver or (in modern systems) the terminal emulator. It happens independently of Java ... or whatever else the application is written in ... unless the application has turned character echoing off or something.

So ...

We need you to explain how you've run the application that is causing the console connected to System.in to get into "no echo" mode.


I don't know if this is the cause of your problem, but your readLine method is incorrect. Each time you call readLine it creates a new InputStreamReader and BufferedReader for System.in. This is inefficient. But worse than that, it is liable to lose input. You see, when in.readLine() is called, the input stack will make a read call on System.in to read all input that is currently available. If more than one line is available (because the user has typed ahead ... or because the application has been called with standard input redirected from a file), you can end up with multiple lines in the BufferedReader's buffer. The readLine() call returns the first line ... but the rest get thrown away.

There is also a bug in the way that you handle EOF. The in.readLine call will return a null when it sees EOF, but you are not dealing with it.


I suspect that the reason you are not getting echoing is something to do with the way you are running the program ... using Ant. Try running your program directly from the command line instead; i.e. run java StatLibraryTest.


Finally, there are some style problems with your code:

  • You've got an identifier (CurrLine) which violates the Java identifier name rules.
  • The indentation is inconsistent
  • Your use of embedded whitespace is inconsistent
  • Catching Exception is generally a bad idea. Catch the specific exceptions that you are expecting (e.g. IOException).
  • It is better to let an exception propagate if your code cannot do something sensible with it. Specifically, if readLine catches an exception, the caller will get an empty String. It can't distinguish that from valid input; e.g. the user entering an empty line. You should declare readLine as throws IOException.

Upvotes: 3

Related Questions