Public Group

# Cant find variable error

This topic is 2599 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm using a book to learn Java, and for one of the exorcizes I came up with a different solution than the book, so I can't figure out on my own what the problem is. It seems that I defined the variable in question in the initialization of the 'for' loop, but it cant find the variable. Also it doesn't seem to have a problem with the exact same code earlier in the program.
Here's the code:
class DayLister { public static void main(String[] arguments) { int yearIn; countDays(yearIn); } static void countDays(int year) { for(int month = 0; month <= 12; month ++){ switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: for (int day = 0; day <= 31; day ++) System.out.println(month + "/" + day); break; case 4: case 6: case 9: case 11: for (int day = 0; day <= 30; day ++) System.out.println(month + "/" + day); break; case 2: if (year % 4 == 0) for (int day = 0; day <= 29; day ++); System.out.println(month + "/" + day); if ((year % 100 == 0) & (year % 400 != 0)) for (int day = 0; day <= 28; day ++) System.out.println(month + "/" + day); break; } } } }
Here's the error:
DayLister.java:30: cannot find symbol symbol : variable day location: class DayLister System.out.println(month + "/" + day); ^ 1 error 

I realize, now that I have looked in the book, that this is not the most efficient way of doing this, but I want to learn how this works before I start on the books solution.

Thanks
Peter

##### Share on other sites
I think you need some code braces

I am not familiar with java but try
 case 2: if (year % 4 == 0) { for (int day = 0; day <= 29; day ++); { System.out.println(month + "/" + day); } if ((year % 100 == 0) & (year % 400 != 0)) { for (int day = 0; day <= 28; day ++) { System.out.println(month + "/" + day); } } break;

I found a yahoo answers reply that deals with this in surprising detail.

Actually thats javascript but I think java goes by the same principals.

##### Share on other sites
Actually, it looks like your error might be in the first few lines of code.

class DayLister { public static void main(String[] arguments) { int yearIn; countDays(yearIn); } 

yearIn wasn't initialized. Cross reference this section of your code with the book's code and see if that's where your problems lie.

EDIT: yewbie is probably right too, though. Blocks '{ }' can get you in Java.

##### Share on other sites
Oh, yea that was it, thank you, Adderly

##### Share on other sites
There's an extra semicolon (";") at the end of the first for in the case "2" block...

##### Share on other sites

There's an extra semicolon (";") at the end of the first for in the case "2" block...

Good eye, Rattenhirn.

##### Share on other sites

Actually, it looks like your error might be in the first few lines of code.

class DayLister { public static void main(String[] arguments) { int yearIn; countDays(yearIn); } 

yearIn wasn't initialized. Cross reference this section of your code with the book's code and see if that's where your problems lie.

Java will default initialize it to 0. This is still a problem, but the semicolon in case 2 mentioned earlier is why it can't find day.

• 48
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631379
• Total Posts
2999666
×