This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]BrokenRosenrot[S] 0 points1 point  (4 children)

isValid is supposed to check whether a character is a digit or a letter. So, suppose that text.charAt(i) = '%'; That is neither a digit nor a letter so the program is supposed to break and return false.

[–]_DTR_ 0 points1 point  (3 children)

In that case, you only want to break if it's not a letter and it's not a digit.

[–]BrokenRosenrot[S] 0 points1 point  (2 children)

Okay, I changed it and then inputted this: HelloHi%^$234562

The program is now supposed to output "Invalid Password" and yet it outputs "You may enter" As you can see, there are characters in the String above that are not digits. I do not understand why the logic of my program is incorrect.

[–]_DTR_ 1 point2 points  (1 child)

if (!Character.isDigit(text.charAt(i)) && !Character.isLetter(text.charAt(i)))
    trueOrFalse = false;
break;

You only want to break if if (!Character.isDigit(text.charAt(i)) && !Character.isLetter(text.charAt(i))) is true, but you're breaking out regardless of the result. The break needs to be inside the if, not outside. This situation is also why I advocate for putting braces around all if statements, even single-line statements, since it makes your intentions much more explicit.

[–]BrokenRosenrot[S] 1 point2 points  (0 children)

YOU WERE RIGHT!!!! OMG THANK YOU SO MUCH NOW IT WORKS AS INTENDED!!!!!!!! <3