简体   繁体   中英

do-while loop doesn't seem to work (Java)

I think I found a solution to verify if a user-submitted string is in the correct format by using a do-while loop which executes as long as a boolean variable has the false value, however, after setting a breakpoint a saw that it exists the loop even though the variable is still set to false. This is the code:

do {
    if (isLegit(x)) {
        try {
            dayTime = sdf.parse(x);
        } catch (Exception ex) {
            System.out.println("an exception was caught. oupsie. the day is now set to today.");
        }
        verify = true;
    } else {
        System.out.println("the format was wrong. please try again");
        x = in .nextLine();
        verify = false;
    }
} while (verify = false);

Boolean isLegit(String x) {
    try {
        if (Integer.parseInt(x.substring(0, 2)) > 0 && Integer.parseInt(x.substring(0, 2)) < 13) {
            if (Integer.parseInt(x.substring(3, 5)) > 0 && Integer.parseInt(x.substring(3, 5)) < 32) {
                if (Integer.parseInt(x.substring(6, 10)) > 1969 && Integer.parseInt(x.substring(6, 10)) < 2016) {
                    return true;
                }
            }
        }
    } catch (Exception ex) {
        return false;
    }
    return true;
}

What should I do about it?

The problem is here:

while(verify=false);

Change it to:

while(!verify);

while(verify=false)更改为while(verify==false)

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