0

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?

5
  • Why do you want to remove it? Commented Oct 27, 2011 at 15:34
  • I'm probably not describing this clearly, I mean, so that at the bottom of all the output there's a > and you'd type commands on the right side of it, so it looks like a prompt. Commented Oct 27, 2011 at 15:35
  • 1
    If that > 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. Commented Oct 27, 2011 at 15:38
  • So far, @G_H has the best suggestion. Feel free to post it as a answer G_H, Commented Oct 27, 2011 at 15:59
  • Yes, it works nearly flawlessly, at least for my purposes. Commented Oct 27, 2011 at 16:09

3 Answers 3

6

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. 
Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

Comments

0

I found a good library, JLine, does exactly what I want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.