Is it possible to print text that can be edited by the user (for console programs)?

Let's say I allow the user to edit something, such as the phone number in the address book (in fact, exactly what I'm doing). Is there something I can add to println that allows me to insert a variable displayed as fully editable text? The assignment that I am doing for this does not actually require it, but I think it would be great to have. I look at Google, but I can’t find anything, then again I don’t know what I’m looking for, and if I have the right conditions ...

+3
source share
3 answers

No, do not use only what Java provides within the framework. To edit some text you will need

  • act when a key is pressed, which is impossible, since in Java the input is buffered (i.e. wait for Enter to be pressed)
  • to navigate in the output text, which is also impossible

This can be done using native code (ncurse on linux, ...) using JNI or JNA, but not so easily.

Please note that there are some projects that are aimed at adding these features, so if you can use something outside of the main libraries, you can give them a try ... for example http://code.google.com/p/ java-console-api /

+3
source

There are various options for this: simplicity and portability of functions and complexity:

+1

If you opened a window similar to the console window and could respond to keystroke events, then you could do what you ask, but otherwise, if you just start the program, the program will stop executing and return control to your console therefore he cannot do anything.

But if you are using a scripted version of java, you can write your own shell, and then you can do what you ask, since the shell will not stop running.

But it will probably be outside of your course.

0
source

Source: https://habr.com/ru/post/1719844/


All Articles