Java - Why is this an infinite loop and how can I fix it?

1.6k views Asked by At

Here is small snippet of a program I'm currently working on where the infinite loop is occurring. I have set all the variables to the values they are at in the program when it encounters this loop. The goal of this loop is to get it to either break (which it won't with these values of course) or to go through the entire loop and then move on, which I believe it should based on the conditions set in the for loop, but instead it becomes an infinite loop. I am lost and wondering if someone could help me. Why is this an infinite loop and how can I fix it? Thanks.

The first code snippet is the minimal code to show the infinite loop. The place where I have put the System.out.println("Infinite"); is where the infinite loop is occurring. It never reaches the if statement in the for loop. I have tried debugging to no avail.

int e = 1;
int f = 0;
int q = 2;
int posInt = 10;
int[] Prime = new int[posInt];
Prime[0] = 2;
Prime[1] = 3;
Prime[2] = 5;
int stay = 0;

while(stay == 0){
    while(e <= posInt){
        if((q + 1) == Prime[f]){
            stay++;
            System.out.println("BREAKING");
            break;
        }
        e++;
        f++;
    }
    q++;
}

Here is the entire program so far in case it helps. The goal is to test whether or not a number entered by the user is a prime number by using the Sieve of Eratosthenes. I realize there may be other errors in the program and it may not be right, but I'm still working on it. Currently I cannot progress though as I am stuck in this infinite loop.

    int posInt;
    Scanner reader = new Scanner(System.in);

    System.out.print("Please enter in any positive integer greater than one to see if it is a prime number: ");
    posInt = reader.nextInt();
    System.out.println();

    int n = posInt;
    int q = 2;
    int a = 3;
    int b = 1;
    int[] list = new int[n];
    list[0] = 2;

    while(a <= n){
        list[b] = a;
        a++;
        b++;
    }


    int c = 2;
    int d = 0;
    int P = 0;
    int nP = 0;
    int[] notPrime = new int[n];
    int[] Prime = new int[n];


    while(c <= n){
        if(list[d] == q){
            Prime[P] = list[d];
            P++;
        }
        else if(list[d] % q == 0){
            notPrime[nP] = list[d];
            nP++;
        }
        else{
            Prime[P] = list[d];
            P++;
        }
        c++;
        d++;


        System.out.println("Primes numbers: ");
        int y = 1;
        int z = 0;
        while(y <= P){
            System.out.println(Prime[z]);
            y++;
            z++;
        }

        System.out.println("Numbers that are not prime: ");
        y = 1;
        z = 0;
        while(y <= nP){
            System.out.println(notPrime[z]);
            y++;
            z++;
        }


        int stay = 0;
        int e = 1;
        int f = 0;
        while(stay == 0){
            while(e <= posInt){
                if(!((q + 1) == notPrime[f])){
                    stay++;
                    break;
                }
                e++;
                f++;
            }
            q++;
        }
    }

    int g = 2;
    int h = 0;
    boolean prime = false;
    while(g <= n){
        if(posInt == Prime[h]){
            prime = true;
            break;
        }
        g++;
        h++;
    }
    if(prime = true){
        System.out.println("The number " + n + " is prime.");
    }
    else if(prime = false){
        System.out.println("The number " + n + " is not prime.");
    }


    reader.close();
2

There are 2 answers

9
CupawnTae On BEST ANSWER

The infinite loop isn't in the first snippet, it's in the surrounding while - if the break never happens, stay will also be always 0, and unless one of the other operations throws an exception you'll be stuck there forever...

    int stay = 0;
    while(stay == 0){
        int e;
        int f = 0;
        for(e = 1; e <= posInt; e++){
            if((q + 1) == Prime[f]){
                stay++;
                break;
            }
            f++;
            System.out.println("Infinite");
        }
        q++;
    }
0
NerosE On

You need a way to stop your while loop. So declare int f outside the loop and put it in the while condition:

int stay = 0;
int f = 0;
while (f < posInt && stay == 0) {
    int e;
    for (e = 1; e <= posInt; e++) {
        if ((q + 1) == Prime[f]) {
            stay++;
            break;
        }
        f++;
        System.out.println("Infinite");
    }
    q++;
}

And you'll have to look at the logic in the rest of your code (as stated by yourself)