Search code examples
javagroovydsljava.util.scannerbasic

Issue with Scanner class


I am using Groovy to build a DSL that will handle BASIC scripts.

I have a line like this:

100 INPUT V

Which is picked up by a regex which injects some code into the script being executed:

    def retString = "scan = new Scanner(System.in);"
    retString += "${matcher[0][1]} ="
    retString += "scan.nextLine()\n"
    return retString

This code is duly executed but never returns (i.e. the Scanner never returns from nextLine() and so a value for V/matcher[0][1] is not returned, instead the code is stuck waiting for keyboard input no matter how many times I hit the enter key.)

What have I vote wrong here? The input is echoed in a TextArea as it is being typed (i.e. this is the focus at the time) - is it related to that?


Solution

  • In the end this appears to be one of those (rare) areas where BASIC has more expressive power than Java or Groovy - to solve this I had to alter the windowing code (adding a JTextField to accept input), write an ActionListener and implement some latching. Not particularly difficult but about 25 lines of code in comparison to BASIC's 1.