• Post Reply Bookmark Topic Watch Topic
  • New Topic

can I change this to a while instead of a do?  RSS feed

 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So do/while loops are sloppy. Can anyone help me fix this?



I need to validate a bunch of other input similarly... so if there's a better technique I'd like to implement it before I put do/whiles everywhere.

Thanks!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A while loop isn't really any better. In fact, it adds more unnecessary code. For a beginner, I think what you wrote is pretty clear. I would put it in a method though.



A more advanced approach if you have a ton of validations is to create an interface with isValid() as a method. Then you only need one loop that you can call. And implement isValid() for names and other validations.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I don't know what would make a do/while loop "sloppy" when it meets the requirements. Making the process generic would clean things up considerably, though; as Jeanne said making an interface that takes the prompt and validates the input would be pretty slick.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, all the things that need to be validated are different. I made it as generic as possible with a method for each type of validation. Some of the things allow numbers, one is a number in a range, the names are character only and less than 20 characters.... ugh. So making a method for each one, I'm not sure how that would work out because it's so many different types of things.

Thanks for your help!

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you already have a method for each one. What we're saying is that by doing something like this (sorta pseudo-code)So the get method takes a prompt and something that validates. The loop is wrapped up in the get method, which calls the validate method, and loops until it's ok (or whatever condition you need happens). You wouldn't need to define the validator in-line, it could just as easily be an external class that implements Validator, and so on.

This abstracts the loop out of the mainline code. (Taken further, the InputGetterer itself could implement an interface so that the input could be gotten in any number of ways, but let's not go crazy.)
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne is right; the only way to get the same functionality is to extract the body outside the loop once:
As you see, this is code duplication which is not only more tedious to write, but also error prone. If you want to add a statement to the loop you must also add it before the loop, and that is quite easy to forget.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to your original question is "yes". You can change every kind of loop (with some very rare exceptions) to a "while". Even though in your instance a do-while sounds more appropriate.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!