Home > Back-end >  How to end a infinite while loop with user input
How to end a infinite while loop with user input

Time:11-28

I am trying to create a loading sequence of 3 dots that repeats itself until input from the user breaks the loading sequence specifically the enter key. i connot for the life of me get the infinite while loop to end with input

public class loop {

    public static void AnyKey() {
        try {
            System.in.read();
            loading(false);
        } catch (Exception e){}
    }

    public static void pause(long duration) {
        try{
            Thread.sleep(duration);
        } catch (InterruptedException e){}
    }

    public static void loading(boolean status){
        if (status == true) {
            while (status) {
                pause(500);
                int i;
                for (i = 0; i <3; i  ){
                    System.out.print(".");
                    pause(500);
                }
                System.out.print("\b\b\b");
            }
        }
    }

    public static void main(String[] args) {
        loading(true);
        AnyKey();
    }
}

CodePudding user response:

In your current code, the main method calls loading and never leaves the function. If you go through loading(true) step by step, you find that since while(status) is always true you are stuck there and AnyKey() is never called.

Also, System.in.read(); is a blocking call. This means that you will wait for user input but will be unable to print the '...'. Instead I recommend your read the documentation for input stream, there you will find the .read() function but also the .available() function which will let you know if any characters have been entered in the input buffer.

Those should be all the tools you need to figure this one out (I think).

Hope this helps!

CodePudding user response:

I figured it out i needed to learn about and use Threads and global variables check out my code below im fairly pleased with myself i was working on this for 3 days now lol

import java.util.Scanner;
class Anykey extends Thread {

    public void run() {
        Scanner scanner = new Scanner(System.in);
        scanner.nextLine();
        System.out.println("end loop");
        loops.loadingStatus = false;
    }
}
public class loops {
    public static boolean loadingStatus;
    public static Scanner scanner = new Scanner(System.in);

    public static void AnyKey() {
        scanner.nextLine();
    }

    public static void pause(long duration) {
        try {
            Thread.sleep(duration);
        } catch (InterruptedException e) {}
    }
    public static boolean loading(){
        loadingStatus = true;
        while (loadingStatus) {
            pause(500);
            int i;
            for (i = 0; i < 3; i  ) {
                System.out.print(".");
                pause(500);
                if (!loadingStatus){
                    break;
                }
            }
            System.out.print("\b\b\b");

        }
        return loadingStatus;
    }

    public static void main(String[] args) {
        Anykey anykey = new Anykey();
        anykey.start();
        loading();
    }
}
  • Related