2

I am trying to emulate this,

$ are you sure (y,n)? y

Where if the user hits enter, the default option would be read from nextLine() using the Scanner (default being the value already populated in the System.in which is in the above example the String y).

if the user wants to change the value he should hit (backspace/delete) and enter a new value.

is this scenario possible using the Scanner and System.in?

This question is not the same, I want to be able to write to System.in so when I do nextLine() to the Scanner object it will return the default value I have wrote. it is something I always see in bash scripts. In that question he is just outputting the default value within the printed string and then defaulting to it if the user hits enter on an empty input.

This is the closest question I have found, and the answer is saying it is impossible to do.

What do you think?

EDIT1: Read Tom's comment below.

There is no benefit in pursuing such effect.

Community
  • 1
  • 1
Faris
  • 875
  • 1
  • 9
  • 18
  • I am quite sure that i have seen this question beeing asked a few hours earlier. Did you remove it? – SomeJavaGuy Oct 21 '15 at 11:35
  • Also `if(intput.equals("")` should match your condition for a default value, since an empty should equal an enter input. – SomeJavaGuy Oct 21 '15 at 11:40
  • Agreed, but it would not be the same as having the value 'y' written in the console – Faris Oct 21 '15 at 11:47
  • 3
    Well, most of the programs use this behaviour: "Would you do the thing? [yes]: " there an empty input means, that the user wants to use the default. So you really want something different? And no, this is not impossible, but the implementation could be a code smell. – Tom Oct 21 '15 at 11:55
  • I think I have to agree with you Tom.. no point in trying to do it. – Faris Oct 21 '15 at 12:01

0 Answers0