You could use some terminal control characters. So once the user has entered their choice you move the cursor backup to the line where the prompt was, then to the left until you get to the point where the user started typing. Then just print spaces, or as I have in this example, stars over the input.
Code:
package rsp;
import java.io.*;
public class Rsp {
public static void main(String[] args) throws Exception {
String prompt = "Enter command: ";
// Setup a reader for stdin.
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
// Prompt the user for a command.
System.out.print(prompt);
System.out.flush();
// Read the users command.
String command = stdin.readLine();
// Move cursor back up to the previous line.
System.out.print("\u001b[A");
// Move cursor to the end of the prompt, char-at-a-time.
for (int i = 0; i < prompt.length(); ++i) {
System.out.print("\u001b[C");
}
// Blank the chars enter by the user. I'm using '*' but you could use ' '.
for (int i = 0; i < command.length(); ++i) {
System.out.print('*');
}
// Move back to the next line.
System.out.println();
// Make sure commands are sent to the terminal with a buffer flush.
System.out.flush();
}
}
Just one small note with this solution, it requires a compatible terminal. Any terminal on Linux/UNIX will work. The terminal in NetBeans (if you are using it) is not compatible and will print some strange output if the program runs in it.
Output looks like this in a compatible terminal:
Code:
Enter command: this is my command.
when the user hits enter it is changed to:
Code:
Enter command: *******************
Bookmarks