This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Having trouble looping and checking a string for a digit  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is for a password verifier homework assignment. I need to loop until the password meets the criteria. Right now my program keeps looping even after it accepts the password. I've tried a break statement but then it just wouldn't loop at all. I also cant seem to figure out how to check if a string contains a digit. if anyone could offer any advice as to what im doing wrong it would be greatly appreciated.

       
 
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First:


That's actually assigning the value true to validPassword.  The result is that this will always be true, and so the loop will never end.
Since validPassword is a boolean, however, you should not be comparing it like that, you just need to use validPassword:


However, if you look at your do-while code, where in there are you changing the value of validPassword?
 
Marshal
Posts: 63843
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

DT is right. Never write == true nor == false which are both poor style and error‑prone, as you have seen.
Never while (b == true) ...
Always while (b) ...
Never while (b == false) ...
Always while (!b) ...
Never if (b == true) ...
Always if (b) ...
Never if (b == false) ...
Always if (!b) ...

What is more, your loop will continue to run while you have a good password, and will stop when you have a bad password (line 18).
Maybe you should loop through the individual chars in your password and test them with a method of the Character class. Maybe you should try something different . . . .
 
Lor Sturuda
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for pointing that out. I guess I didn't realize I was assigning the value. that makes sense now, but I still can't seem to get my loop right. Any way I change it, it either stops when it shouldn't or keeps looping when accepted. I'll keep playing around with it. So do you think it would be best to put each test in a separate method? Thanks
 
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lor Sturuda wrote:So do you think it would be best to put each test in a separate method?



I think it could be a good idea. Right now it looks like you wanted to write a loop to check for digits in the string, but it's getting mixed up with the code which does the other checks. So you haven't written the loop yet. If you wrote a method boolean hasADigit(string) then you could just write that test and you wouldn't have the other tests making trouble.
 
Sheriff
Posts: 5813
150
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like in line 13 you aren't setting validPassword when you call the method verifyPassword.
 
Paul Clapham
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:It looks like in line 13 you aren't setting validPassword when you call the method verifyPassword.



Lor probably thinks that setting the validPassword variable inside that method will do that. However it's declared as a local variable inside the method so it doesn't set the value of the variable which is declared at the top of the posted code fragment.
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!