2

One of my final project requirements is that, if it is console based which mine is, entering ctrl + z WON'T mess up the program. The nextLine() statement from my scanner is within a while loop, and when I try to catch the NoSuchElementException from entering ctrl+z, the error message in the catch block loops endlessly.

        while (!advanceTurn) {

        System.out.print("\nCommand: ");

        try {input = s.nextLine();}
        catch (NoSuchElementException e) {

            System.out.println("Use the EXIT command to exit the game.");
            s = new Scanner(System.in);
            input = "asdfjkl;";
            continue;

        }

        System.out.println();

        // This takes the input and decides what command was entered (if any) and deals
        // with it appropriately.
        parseCommand(input);

    }

As you can see in the code, I've tried to re-initialize the scanner when the error is caught, and then jump back to the beginning of the loop so a new line can be grabbed, but it appears that the error still persists even when there's no new input from the keyboard, so I'm pretty much stumped as to how to solve this. Any help is appreciated, thanks.

EDIT: I'm using Eclipse IDE if that helps.

Developer Guy
  • 2,318
  • 6
  • 19
  • 37
theolaa
  • 33
  • 8
  • On windows, ctrl-z terminates the process. On other platforms, it sends the process to the background. Eclipse may not be consistent with actual terminals. Why are you entering ctrl-z? – Elliott Frisch Apr 05 '18 at 07:41
  • Every console implementation handles this differently. Pickup just one and code it accordingly. I had tried powercmd, cmd and eclipse console in back and they behaved differently – Shailesh Pratapwar Apr 05 '18 at 07:42
  • @ElliottFrisch Ctrl-Z doesn't terminate the process, it sends an EOF to stdin. However, that will of course close the input stream and you won't be getting it back. – daniu Apr 05 '18 at 07:43
  • It's about catching the kill event. In java we can do it with shutdown hooks. – Tamas Rev Apr 05 '18 at 07:45
  • Possible duplicate of [How to gracefully handle the SIGKILL signal in Java](https://stackoverflow.com/questions/2541597/how-to-gracefully-handle-the-sigkill-signal-in-java) – Tamas Rev Apr 05 '18 at 07:45
  • @ElliottFrisch I'm entering ctrl-z to make sure it doesn't mess up the program as per the requirements. If I don't surround the String = Scanner.nextLine() statement in a try catch, I get a NoSuchElementException. When I try to catch it, I get the loop described above. – theolaa Apr 05 '18 at 07:46
  • I'd just declare "Use EXIT command or Ctrl-Z (Ctrl-D on Unix) to exit the game" as the exit condition, so it won't be "messing up your program" but terminating it gracefully. – daniu Apr 05 '18 at 07:46
  • The specific requirement is: Erroneous input must not terminate the game. A typing error should result with an appropriate error message and the game continuing. If you use the console for input, make sure that Ctrl-Z does not cause an error. When I try to catch the error, It does the looping as described above. – theolaa Apr 05 '18 at 07:48
  • @TamasRev Ctrl-Z is not a SIGKILL, it's a SIGQUIT. – daniu Apr 05 '18 at 07:49
  • @daniu - it's a SIGSTOP on Linux, isn't it? – Brian Agnew Apr 05 '18 at 15:01

1 Answers1

1

I talked with my instructor today and we fixed the problem by jumping outside of the while loop with a return statement in the catch block, and then initializing s as a new scanner just before entering the loop again. Since the purpose of this snippet is to repeatedly get commands until certain commands that constitute the end of a turn are entered, the while loop is entered every time a command is be entered, and it now works just fine for me.

theolaa
  • 33
  • 8