• 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

Problems with date validation code

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any help would be greatly appreciated, and if you find anything wrong that the compiler missed, feel free to point it out.
Here is the code I have so far that I can't get to compile because of one error:

LegalDate.java:30: error: variable loop is already defined in method main(String[])
char loop = aString.charAt(0);
^
1 error

 
Saloon Keeper
Posts: 10687
85
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
Lines 10 and 30 define the same variable. This is not allowed.
 
Philip Hager
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but when I delete the one at the top I get this error:

LegalDate.java:13: error: cannot find symbol
while (loop != 'N'){
^
symbol: variable loop
location: class LegalDate
1 error
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks to me like those two variables are supposed to be one variable used for a single purpose. Therefore you only need to declare it once, not twice.

When you removed one of the declarations you had a problem. But that's because you removed the wrong one.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 30 - change

to
 
Philip Hager
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so that problem is fixed, thanks.

Now I'm having an two issues:

First - The code isn't correctly validating dates
Second - It gets stuck in an infinite loop

This is the relevant code for the first problem (how can I set the char loop variable to the input Y/N from user):


Here is the code for the second issue:
 
Philip Hager
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First problem has been fixed, only second problem remains.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Philip Hager wrote:This is the relevant code for the first problem (how can I set the char loop variable to the input Y/N from user):



You had a line of code which did that, but you deleted it. Don't delete the part of the code which does what you want, just delete the part which makes it a redundant declaration.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Philip Hager wrote:Here is the code for the second issue:



There weren't any loops in that code, so that code couldn't possibly be causing an infinite loop. The cause for your infinite loop must be elsewhere.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You took out the assignment to loop below line 3.
 
Philip Hager
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The loop problem has been fixed, and I substituted the switch command for the if command when validating the days in a month, but I still get an output of "The date is valid and is not in range."

The code is supposed to check the year to be in a specific range, if any of them are out of that range that is the only time it should give me the "The date is valid and is not in range", but I get it all the time.

Updated Code:
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I'm not clear about what is the difference between "valid" and "in range". It looks like your variable notValidDate is somehow supposed to tell you whether the date is "in range", or maybe "not in range", but it would help if you wrote some code which assigned it a value other than false.

I think my problem arises from two variables named "validDate" and "notValidDate" which keep wanting to be opposites of each other in my mind, but apparently they are supposed to be independent of each other?
 
Philip Hager
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The boolean for isValidDate works as intended, but the one for isNotValidDate does not when used with this part of the code:

 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you talking about "isNotValidDate" or "notValidDate"? I ask because the line of code you posted uses "notValidDate", so if that isn't what you expected you should look at the lines of code which assign values to it.
 
Philip Hager
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, I got it to work as expected now, not sure why it works correctly this way, but it does. Thanks for all the help!

Updated code once again:

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Philip Hager wrote:Alright, I got it to work as expected now, not sure why it works correctly this way, but it does.


Well done! A few minor things for you to consider:

1. isNotValidDate() should probably be called invalidYear(). Ditto for the variable you assign it to.

2. You could make your numDaysInMonth() method a lot more compact by storing month lengths in an array, viz:
3. You don't need to go overboard with brackets (although I understand why you did). Also: Java has a '!=' operator, so:
  return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
will work just fine, and may be a bit clearer.

4. If you took the components in year, month, day order, you could check that each was valid when it was entered.
I realise that probably wasn't the exercise here, but you might find this page useful for some general tips on user input.

HIH

Winston
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Philip Hager wrote:. . .

That should read Don't stand for any nonsense about wrong parameters; if there are only twelve months let them know they have got it wrong rather than quietly returning 0.
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic