1

Dunno if it's possible, but would it be possible to add a Event Listener for text input, so that instead of having to check the input to see if the user typed anything that triggers an action, I can just invoke an Event Listener.

Instead of something like

Scanner sc = new Scanner(System.in);
if(sc.nextLine().equals("!help"){
    // do something
}else{sc.nextLine().equals("y"){
    // continue
}

I can just declare an ActionListener to Scanner inputs.

Dragonsnap
  • 834
  • 10
  • 25
  • 2
    When you read the docs for `Scanner` did you see anything like that? Why not just wrap it in something that fires an event on input if that's what you really want? – Dave Newton Jul 31 '19 at 12:04
  • You cannot add action listener to a Scanner. Actions are for GUI. You should rather build a GUI with textfield, then you cann add action listeners on it. – taktak Jul 31 '19 at 12:07
  • You might never want to use `sc.nextLine()` inside `if-else if` conditional statements. It will keep reading the input and wouldn't achieve what you want to do with it. – Mushif Ali Nawaz Jul 31 '19 at 12:10
  • For without GUI look here https://stackoverflow.com/questions/27381021/detect-a-key-press-in-console – Anatoly Jul 31 '19 at 12:21
  • You could create an object that wraps the `Scanner` and fires its own events when appropriate. This will likely require the use of a separate thread. – Slaw Jul 31 '19 at 13:42

0 Answers0