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
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.