• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Date Validation

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is my current program for my java class. The assignment is to validate a date when entered by user.


My issue with my program is the output for the day.
If I enter 30 for the date I get this: Date is invalid
Date is invalid
Date is invalid
Date is invalid
Date is invalid

I wanted to know how what is wrong and why it is doing this.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, and welcome to the Ranch!

First, if you're going to post code, please UseCodeTags ←click. They make it easier to read your post. I've added them for you this time.

Now to your question. You should review this page: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html because your boolean expressions are not working the way you probably think they're working. In particular, && takes precedence over || in a boolean expression, much like how * takes precedence over + in an arithmetic expression. So, an expression like (a || b && c) is evaluated as (a || (b && c)), not ((a || b) && c).
 
Kara Wilimas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hello,

Thank you for that.

I added the parenthesis but when an int variable for day is entered it appears to run through all four booleans and not just the one that applies. So I get four outputs of either "Date is valid" or "Date is invalid"

How do I fix this problem?

 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, if you were wondering why your program prints out "Date is invalid" multiple times, it's because your if-else statements are all executed independently of each other. That is, each one of them will execute regardless of what already happened with any previously executed if-else statements. You could chain your if-else statements to make them dependent on the results of previous ones.

This code will always result in two actions being executed: (action1 or action2) and (action3 or action4)

In order for action4 to execute, only condition3 has to be false; it doesn't matter what condition1 is.

The code below will result in exactly one of action1, action2, action3, or action4 being executed:

In order for action4 to execute, (condition1 && condition2 && condition3) has to be false. Do you see the difference?
 
Kara Wilimas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Yeh I understand. That was actually something I was thinking of doing.


Okay so I think I fixed that problem but now it is telling me
Date.java:91: error: illegal start of statement
at the end of my code and I checked all my brackets and they are paired so I don't what is wrong.

 
Kara Wilimas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uhhh....okay never mind. It is now working o.o
 
Marshal
Posts: 80627
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

You need a little thought about repeated if‑elses. You can convert something like this to the second version, which is much neater and easier to read:-
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing you can do to clean up your code is to remove repetitions. For example, System.out.println("Day is valid") is repeated multiple times in your code. You should separate logic to check validity from your display statements. Your code will be cleaner and clearer if you do something like this:

I suggest you try to restructure your code this way and compare it to your original version. I think you'll find that this way is a lot easier to understand and it looks a lot cleaner. There are other ways you can clean it up but this is a good start. Baby steps.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic