1

I'm aware of various methods for reading a String from the user's keyboard in Java (Scanner, Console, BufferedReader), however, none of them seems to be capable to accept a given String that the user can edit.

To rephrase it in code, I'm looking for something like this:

Scanner sc = new Scanner(System.in);
System.out.print("Please edit as you like: " + s);
s = sc.nextLine(s);

As this seems so simple, am I overlooking something? Or is this really not possible in Java without a GUI?

Oliver Hummel
  • 121
  • 2
  • 4
  • I found this how to inject a text to System.in: https://stackoverflow.com/questions/3814055/writing-data-to-system-in, however, it does not allow to edit the text. – Oliver Hummel Nov 17 '19 at 19:20

2 Answers2

1

Doesn't look like it will be possible. There are similar posts discussing this. Java: populating Scanner with default value on Scanner.nextLine();

A few options have been discussed in there but even the people providing those options are clear that it doesn't meet the stated requirment

Denounce'IN
  • 95
  • 1
  • 10
  • Yes, the question goes into the right direction, unfortunately, the answers are either not helpful or completely discouraging. At least it looks as if I haven't overlooked something. Still open for creative solutions though ;) – Oliver Hummel Nov 16 '19 at 12:45
  • JCurses (java port of Unix curses) might help. I'm not saying it will. But worth investigating as you get a lot of control over your terminal. But it is more of a rendering tool i.e. tool to write to a very specific position on the terminal than a channel of input . But, there may be a way given you have a finer level of control. JCurses could be used to generate things like progress bars that need to repeatedly write on the same row on the terminal by changing column position back and forth. Something like Unix top command. My 2 cents as you seem so determined to get this done – Denounce'IN Nov 16 '19 at 14:08
  • I'm looking for something simple to use for an exercise with absolute programming beginners. Hence, JCurses (and AsciiPanel that I found while googling the first) is probably not the very best solution either... :-( thanks anyway for your help. – Oliver Hummel Nov 17 '19 at 18:59
0

If you are trying to edit an existing String, then it's not possible. String is immutable, you can use StringBuilder or create a new String with the new value.

luizfvm
  • 1
  • 1