• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Help with loop problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a password verification routine, my problem is when a input is entered it will go through each if statement individually. I'm trying to restart validation when a condition is not met. At the moment it only jumps to the next if statement when the previous condition is met, even though its a new input. Thanks in advance.
 
Marshal
Posts: 71070
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Well done finding the code button, but you need to indent your code to make it legible. Remember that you can confuse yourself with unindented code.
Why are you caling nextLine() so often (lines 16, 23, 28, 33, 38, and 43)? What will happen if one of the tests fails? I think you will get a new password and then only do part of the validation on it. Or, if you don't enter something, your program will appear to hang. I think those multiple ifs would be better as else‑ifs.
What does line 46 mean? It looks like the end of a do loop, but it means you will never reenter the loop, so your loop will always run once. In which case, what is the point of having a loop in the first place?

Don't write if (something) return true; else return false; See the old Sun Style Guide (ยง10.5.2).Give that method private access. Its name doesn't tell us what it does.
 
Leon Kirby
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the heads up. I'm basically trying to get the loop to re run when 1 of the conditions is not met. At the moment it loops through individually. I tried the multiple next.() to try get back to the start of the verification. Line 46 is also an attempt to re run the if statements when one if the conditions are false.
 
lowercase baba
Posts: 12933
65
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about simplifying things a bit.  What do you really want to do?  You want to keep looping until some condition is false.  so, write that:


now, in my mind, "validate input" is a bit broad to be a line of code...so let's make it a method.  and there is no reason to call the method, save the validity, and then use that in the while statement, so we can simplify the above.  i'm going to call a boolean method in the while :


so now i need to write an isValid() method that returns a boolean.  It needs the string to test.  Note - now i need to update the line where I call this, too...but that's how it goes. You keep adding one piece at a time.



so now I need to fill in that body.  My preference would be to write a single method to test each requirement, then call them one by one.  I would STRONGLY advice writing one at a time, test it, get it working, etc. before you start adding the second. Repeat with testing it, etc. before starting the third.


 
Beauty is in the eye of the tiny ad.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic