Jump to content
  • Advertisement
Sign in to follow this  
destructivArts

Cant find variable error

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!