0

How can i terminate while loop when user press 'q' so program should exit?

It is Simple Java Program and i am not using swing etc.

while (flag) {

                int x = ran.nextInt(MaxValue - MinValue + 1);
                System.out.print(x);
                System.out.print(" enclosed by {");
                for (int i = 0; i < arr.length; i++) {
                    System.out.println(arr[i][0]);
                    if ((x >= arr[i][0]) && (x <= arr[i][1])) {
                        System.out.print("(" + arr[i][0] + "," + arr[i][1] + ")");
                    }
                }
                System.out.print("}\n");
                count++;
            }
3
  • Possible duplicate of How do I stop while loop from running infinitely? Commented Sep 5, 2019 at 19:25
  • @sayan I am looking for keypress and exit not to use scanner to get input..i hope you got it. Commented Sep 6, 2019 at 5:21
  • Just scan for the keypresses and compare with that of the letter 'q'. If it isn't then continue otherwise exit. Commented Sep 6, 2019 at 5:24

1 Answer 1

0
import java.util.Scanner;

public class test {
    public void testFunction() {
        listener threadobj = new listener();//create the object
        Thread thread = new Thread(threadobj);//create the thread
        thread.start();//start the thread. it runs asynchronously

        while (flag /*assuming flag is unrelated*/ 
                && !threadobj.exit) {
            //when !threadobj.exit is false it means that q has been pressed.
            //so flag && !threadobj.exit will be false if at least one is false
            //so the loop will end
            int x = ran.nextInt(MaxValue - MinValue + 1);
            System.out.print(x);
            System.out.print(" enclosed by {");
            for (int i = 0; i < arr.length; i++) {
    System.out.println(arr[i][0]);
                if ((x >= arr[i][0]) && (x <= arr[i][1])) {
                    System.out.print("(" + arr[i][0] + "," + arr[i][1] + ")");
                }
            }
            System.out.print("}\n");
            count++;



        }

        threadobj.killMe = true;//finish its loop incase it is still running :)
    }
}
class listener implements Runnable {
    public boolean exit;    //if true then the q button has been pressed 
    public boolean killMe;  //set to true to kill this thread

    @Override
    public void run() {
        Scanner scanner = new Scanner(System.in);
        while (!killMe) {           
            if(scanner.hasNext("q") || scanner.hasNext("Q")) { //Should trigger when a q is pressed.
                exit = true;        //if not use 'scanner.nextLine()'

                //wait a bit for input, so less cpu is used for constant checks.
                try {
                    this.wait(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }           
        }

    }

    public listener() {
        exit = false;
        killMe = false;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You should not close the Scanner when using System.in, you will find yourself unable to ever open System.in again, until the program is terminated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.