• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confusion with booleans & while/if loops

 
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having a hard time finding the issue in my code. If it picks up that it's an invalid date, I can't get out of the loop by entering a valid date. I've been trying to mess with the operands and starting the booleans with false or true but I can't seem to get the input to be validated correctly.



 
Saloon Keeper
Posts: 10719
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have 3 'if' statements checking the validity of the inputted date but this check only appears outside of the loop. Inside the loop you input a new date but it doesn't validate that date.
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to re-validate month, day, and year inside the loop again, right after the keyboard entries:




Also,  you should put the repeated code inside functions or by changing your logic, so the code is more reusable and less repetitive.


I did the following test:

 
Marshal
Posts: 79231
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, I would suggest you get rid of the if blocks. Let's imagine you have entered day=23 month=11 year=2016 and run through the loop. At that point all your three booleans are set to true.
Let's imagine you now go round the loop again and do the validation again. Let's imagine you enter day=99 month=11 year=2016. The month and year if statements will run normally setting their booleans to true, i.e. the same as before, but the day if statement will never run. So is the day boolean going to be left at true?

I think you are better using a straight assignment:-
day = rentalDay >= 1 && rentalDay <= 31;
Avoid writing magic numbers like 31. Better style is to set up a constant
private static final int MAX_DAYS_IN_MONTH = 31;
0, 1 and −1 are permissible as number literals. Then you write
day = rentalDay >= 1 && rentalDay <= MAX_DAYS_IN_MONTH;
You might consider changing the names of those booleans. When you have rentalDay and day, it is obvious that day is going to be a number. Something like dayValid would sound better. It is a case where you can make life so much easier for yourself with judicious choice of variable names. And you can make life so much more difficult for yourself with injudicious choice of variable names. You might consider a single boolean, maybe dateValid. Then you can try this after you have set dateValid for the day
dateValid = dateValid && rentalMonth >= 1 && rentalMonth <= MAX_MONTH; /* etc. */
Avoid == true and == false like the plague. They are poor style and I shall let you work out the error they are prone to.
Not if (valid == true) ... but if (valid) ...
Not if (valid == false) ... but if (!valid) ...
Note the bang sign ! for not can be difficult to read. And <= or >= are harder to read than < >. Consider changing the name of your boolean. Can the program become easier to read if you don't work out dateValid at all? What if you work out dateWrong?
 
Campbell Ritchie
Marshal
Posts: 79231
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and welcome to the Ranch
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic