Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Code does not exit do while loop

I'm making java program that sets password, prompt to enter password and check if the entered password matches the set password and keep prompting to enter password if the entered password does not match.

However code doesn't exit do-while loop even though boolean statement evaluates to false (in other word, even if I enter correct password). Can anyone take a look and suggest how to fix it?

My code: https://w.trhou.se/jzs4qoc219

1 Answer

In your while loop you create variable pw but don't set a password. So what you are checking against is null. Perhaps you meant:

String setPassword = newPassword.getPassword();

With that fix you also output "Password is wrong, please try again." for a correct password but then the loop exits correctly.

It worked! Thank you so much!