简体   繁体   中英

Infinite while loop involving boolean variable in Java

this is my first post, so apologies if I fail to follow proper formats/conventions etc. on this site. I've only been programming for a few weeks.

Anyways, I am writing a basic Java program using a boolean variable and a while loop. The code is very 'clunky' to say the least and could definitely be made much more elegant (though not with my limited skill set, so again, apologies). If the boolean variable is true, the code works as it should. However, if the variable is false, the code goes to a while loop and even if an input is put in which should make the statement true, the loop just keeps going. Any ideas what's causing this infinite loop. I'm practically certain it's something basic, but I just can't seem to figure it out. Here is the code below. Thanks!

import java.util.Scanner;
import java.util.Random;
import static java.lang.System.out;
import static java.lang.System.in;

public class MB1 {
    public static void main(String args[]){
        char a, b, c, d;
        Scanner myScanner = new Scanner(in);
        boolean secondBoolean;

        out.println("Let's get started! Type in your 4-digit code:");
        a = myScanner.findWithinHorizon(".", 0).charAt(0);
        b = myScanner.findWithinHorizon(".", 0).charAt(0);
        c = myScanner.findWithinHorizon(".", 0).charAt(0);
        d = myScanner.findWithinHorizon(".", 0).charAt(0);

        secondBoolean = ((a == '1'|| a == '2'|| a == '3' || a == '4' || a == '5' || a == '6' || a == '7')
            && (b == '1' || b == '2'|| b == '3' || b == '4' || b == '5' || b == '6' || b == '7')
            && (c == '1' || c == '2'|| c == '3' || c == '4' || c == '5' || c == '6' || c == '7')
            && (d == '1' || d == '2'|| d == '3' || d == '4' || d == '5' || d == '6' || d == '7'));

            while (secondBoolean == false) {
            out.println("The code you typed is not valid. Please type a different code:");
            a = myScanner.findWithinHorizon(".", 0).charAt(0);
            b = myScanner.findWithinHorizon(".", 0).charAt(0);
            c = myScanner.findWithinHorizon(".", 0).charAt(0);
            d = myScanner.findWithinHorizon(".", 0).charAt(0);  
            out.print(a);out.print(b);out.print(c);out.print(d);
            } 
            if (secondBoolean == true){
                out.println('0');
            }
        }
    }

Value of secondBoolean variable has to be recalculated each time.

Replace

secondBoolean = ((a == '1'|| a == '2'|| a == '3' || a == '4' || a == '5' || a == '6' || a == '7')
        && (b == '1' || b == '2'|| b == '3' || b == '4' || b == '5' || b == '6' || b == '7')
        && (c == '1' || c == '2'|| c == '3' || c == '4' || c == '5' || c == '6' || c == '7')
        && (d == '1' || d == '2'|| d == '3' || d == '4' || d == '5' || d == '6' || d == '7'));

while (secondBoolean == false) {
    out.println("The code you typed is not valid. Please type a different code:");
    a = myScanner.findWithinHorizon(".", 0).charAt(0);
    b = myScanner.findWithinHorizon(".", 0).charAt(0);
    c = myScanner.findWithinHorizon(".", 0).charAt(0);
    d = myScanner.findWithinHorizon(".", 0).charAt(0);  
    out.print(a);out.print(b);out.print(c);out.print(d);
    } 
    if (secondBoolean == true){
        out.println('0');
    }
}

with

while (secondBoolean == false) {
    out.println("The code you typed is not valid. Please type a different code:");
    a = myScanner.findWithinHorizon(".", 0).charAt(0);
    b = myScanner.findWithinHorizon(".", 0).charAt(0);
    c = myScanner.findWithinHorizon(".", 0).charAt(0);
    d = myScanner.findWithinHorizon(".", 0).charAt(0); 

    out.print(a);out.print(b);out.print(c);out.print(d);

    secondBoolean = ((a == '1'|| a == '2'|| a == '3' || a == '4' || a == '5' || a == '6' || a == '7')
        && (b == '1' || b == '2'|| b == '3' || b == '4' || b == '5' || b == '6' || b == '7')
        && (c == '1' || c == '2'|| c == '3' || c == '4' || c == '5' || c == '6' || c == '7')
        && (d == '1' || d == '2'|| d == '3' || d == '4' || d == '5' || d == '6' || d == '7'));
    } 
    if (secondBoolean == true){
        out.println('0');
    }
}

You should update value of secondBoolean in each iteration of the loop.


Additionally, you can improve your code's readability bye replacing it with

String choices = "1234567";
secondBoolean = choices.contains(a + "") && choices.contains(b + "")
             && choices.contains(c + "") && choices.contains(d + "");

Good luck

You are in a infinite loop because you're never changing the value of secondBoolean . You should to this inside your while loop.

Also, don't forget to initialize you secondBoolean variable before the loop (setting secondBoolean = false , so your loop will run at least once).

You can try to just read the user's input as a string and split it by "."

import java.util.Scanner;

import static java.lang.System.in;
import static java.lang.System.out;

public class MB1 {

    public static void main(String args[]) {
        char a, b, c, d;
        Scanner myScanner = new Scanner(in);
        boolean secondBoolean;

        out.println("Let's get started! Type in your 4-digit code:");
        String strIn = myScanner.nextLine();

        // Split input by "."
        String[] strAry = strIn.split("\\.");

        // create char array of equal length to String array
        char[] chrAry = new char[strAry.length];

        // convert strings to chars
        for (int i = 0; i < strAry.length; i++) {
            chrAry[i] = strAry[i].charAt(0);
        }

        // (unnecessary) assignment to a, b, c and d
        a = chrAry[0];
        b = chrAry[1];
        c = chrAry[2];
        d = chrAry[3];

        secondBoolean = ((a == '1' || a == '2' || a == '3' || a == '4' || a == '5' || a == '6' || a == '7')
                && (b == '1' || b == '2' || b == '3' || b == '4' || b == '5' || b == '6' || b == '7')
                && (c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7')
                && (d == '1' || d == '2' || d == '3' || d == '4' || d == '5' || d == '6' || d == '7'));

        while (!secondBoolean) {
            out.println("The code you typed is not valid. Please type a different code:");
            // ... read in code again. You possibly want to do this with a method
        }
        if (secondBoolean) {
            out.println('0');
        }
    }
}

Yeah, you enter the loop when secondBoolean is false. You will go round and round in this loop until secondBoolean is set to true, therefore you must update this within the while loop.

On a side not, maybe your code could be tidied up a bit like this;

List<String> nums = Arrays.asList("1", "2", "3", "4", "5", "6", "7");
secondBoolean=(nums.contains(a) && 
               nums.contains(b) && 
               nums.contains(c) && 
               nums.contains(d));

I am only starting java myself, but this looks a little neater! Hope that helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM