So I want to make a java application which, while running, can run custom commands (not windows commands, commands for my application) from the command line, I can already do it, but I'd like to have a prompt such as > where you'd type commands, how can I output > and then remove it when it's time to print something else to that line?
3 Answers
On Java 6, you can use the Console class for that. Specifically, the readLine method. From the API:
public String readLine(String fmt, Object... args)
Provides a formatted prompt, then reads a single line of text from the console.
Comments
I agree with PaoloVictor's recommendation of using Console, but if you're more curious about architecture I might suggest a REPL (Read, Evaluate, Print Loop). Where you would do something like this.
init();
while(1){
System.out.print("myProgram>");
String cmd = Console.readLine(String fmt, Object... args);
evaluate(cmd);
}
>and you'd type commands on the right side of it, so it looks like a prompt.>needs to be removed from the console after executing the command, maybe using the backspace character (ASCII code 8) could work? Seems hacky, but who knows.