Cant find variable error

Started by
5 comments, last by way2lazy2care 12 years, 11 months ago
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
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out
Advertisement
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.

Yahoo Answers
Actually thats javascript but I think java goes by the same principals.
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.
Oh, yea that was it, thank you, Adderly
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out
There's an extra semicolon (";") at the end of the first for in the case "2" block...

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


Good eye, Rattenhirn.

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.

This topic is closed to new replies.

Advertisement