Reputation: 387
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
Reputation: 18072
The problem is here:
while(verify=false);
Change it to:
while(!verify);
Upvotes: 4