This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with date validation code  RSS feed

 
Philip Hager
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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

 
Carey Brown
Bartender
Posts: 2982
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 2982
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 30 - change

to
 
Philip Hager
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
First problem has been fixed, only second problem remains.
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 2982
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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:

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!