Ileana Profeanu
Ileana Profeanu

Reputation: 387

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?

Upvotes: 1

Views: 420

Answers (2)

benscabbia
benscabbia

Reputation: 18072

The problem is here:

while(verify=false);

Change it to:

while(!verify);

Upvotes: 4

Salih Erikci
Salih Erikci

Reputation: 5087

Change while(verify=false) to while(verify==false)

Upvotes: 1

Related Questions