1

Is it possible to create an editable command line prompt in Java?

At the moment my command line looks like the following:

Scanner scanner = new Scanner(System.in);
System.out.format("Enter new value, press Return for keeping old value [%s]:", oldValue);
String newValue = scanner.next();

EDIT:

While this should works under normal circumstances, I likely would have a command line, that populate the old value and let me edit it and finally take the new value on pressing Return; something like an editable TextField for the console.

I have tried jLine2 with something like this:

ConsoleReader reader = new ConsoleReader;
reader.putString("2");
String input = reader.readLine();

...but unfortunately, this hides the value 2 until I press Key "2" and deletes the whole line on backspace... And because of the very poor documentation of jLine2 on Github, I have no idea if this is even possible?

Christian Rädel
  • 581
  • 1
  • 5
  • 13

2 Answers2

0

You could use an external library for that, e.g. https://github.com/jline/jline2/wiki/JLine-2.x-Wiki

D.R.
  • 20,268
  • 21
  • 102
  • 205
  • Don't post link-only answers: if the site you're linking to is down for any reason, then this answer loses any meaning. You should include the relevant information from the site you've linked to in your answer and leave the link as a reference and for further reading. – JonK Jul 08 '15 at 08:41
0

If you are asking to read only full lines from standard input, you can use the solution proposed a while back at: Readlines from console

Community
  • 1
  • 1
crigore
  • 400
  • 5
  • 23
  • Don't post link-only answers: if the site you're linking to is down for any reason, then this answer loses any meaning. You should include the relevant information from the site you've linked to in your answer and leave the link as a reference and for further reading. – JonK Jul 08 '15 at 08:41
  • Does not really apply here ... the link is to another SOF question... or in other words: don't simply spam your answer everywhere ;-) – D.R. Jul 08 '15 at 08:44
  • I could copy the code here, but I didn't want to claim authorship of it :S – crigore Jul 08 '15 at 09:05
  • @crigore As long as you properly credit the original author (and if you feel the need, you can also make your answer a community wiki) then I don't see any problem with it. However, even SO questions/answers can get deleted, so I'd still suggest grabbing the relevant bits - just credit the original author properly. – JonK Jul 08 '15 at 10:52
  • @JonK Ok, I will try doing so. I'm still new in this world. Thanks! – crigore Jul 08 '15 at 11:17