0

I was attempting to code a program that gives you the season based on an inputed month and day, but ran into a problem halfway through. After I intialize the variable month in an if statement, to check if the value inputed is a valid month, I cannot use the variable month later in the code to find the season as it gives me the error "cannot find symbol." Any help would be much appreciated.

import java.util.Scanner;
public class Date 
{
    public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter the number of the month: ");
        if (in.hasNextInt() && in.nextInt() > 0 && in.nextInt() <= 12)
            {
            int month = in.nextInt();
            }
        else 
            {
                System.out.println("Error: Invlaid month value");
            }
        System.out.println("Please enter the day: ");
        int day = in.nextInt();

        String season;
        if (0 < month && month <= 3)
            {
                season = "Winter";
            }
        else if (3 < month && month <= 6)
            {
                season = "Spring";
            }
        else if (6 < month && month <= 9)
            {
                season = "Summer";
            }
        else if (9 < month && month <= 12)
            {
                season = "Fall";
            }

    }

}
jhocon
  • 15
  • 1
  • 1
    You will only be able to access it if it has been declared in a scope greater than equal to the scope that is attempting to access it. More specifically, they scope of the declared variable must be a subset of the scope you are currently in. – Zachary Dec 22 '17 at 01:53
  • Define and initialize it to a default value *outside* and *before* the `if` statement, then when it gets assigned your value in the if statement, it will be accessible throughout the rest of the program. – chickity china chinese chicken Dec 22 '17 at 01:57

1 Answers1

0

The problem you encounter is that you have declared the variable within the if statement, meaning it may only be accessed within the { }. This article goes over the basics of variable scope in Java. You will only be able to access a variable from a scope if the variable was defined in a scope that is a subset of the current scope.

To achieve what you want, you will need to declare the variable outside the if-statement so that it can be accessible. Note you will need to handle the case when month is invalid otherwise you will have the default value of 0.

int month = 0;
if (in.hasNextInt()) {
    month = in.nextInt();

    if (!(month > 0 && month <= 12)) {
        month = 0;
        System.out.println("ERROR");
    }
} else {
    // Handle graceful exit
}

...
if (0 < month && month <= 3) { ... }
Zachary
  • 1,693
  • 1
  • 9
  • 13
  • 2
    That `if` statement looks very dodgy. Every call to `nextInt` will consume an `int` – Scary Wombat Dec 22 '17 at 02:09
  • Well spotted, Wombat. Not sure how I missed that, when all three answers had the same bug. – Dawood ibn Kareem Dec 22 '17 at 02:11
  • Fixed. Completely forgot to check for other issues as the problem was glearingly obvious. Cheers for pointing that out. Fixing it also avoids the problem he was having altogether to be fair... – Zachary Dec 22 '17 at 02:14
  • At this point the structure of the code is significantly different. The purpose of the question is to understand variable scope and I hope that has come across. Fixing the code to avoid the many problems detracts from the question by removing variable scope from being a problem. – Zachary Dec 22 '17 at 02:21
  • 1
    Yes, but you've dealt with that effectively by explaining "scope" in your first paragraph. Although I think "subset" on the fourth line should actually be "superset". Good answer. – Dawood ibn Kareem Dec 22 '17 at 02:22