3

I'm not sure if I am doing this right, but I keep getting an error saying my array "currentRoute" cannot be resolved into a variable. I'm assuming this is because I have declared the array in a different scope. Is there any way I can get this to work while still declaring the variable in within my switch statement and while still using a simple array? (I cannot use an arraylist, as the rest of my code would be affected)

    switch (routeID)
    {
        case "1" : {
            String[] currentRoute = new String[Route1.length];
            currentRoute = Route1;
        }
        case "96" : {
            String[] currentRoute = new String[Route96.length];
            currentRoute = Route96;
        }
    }

    // print out values in currentRoute
    for (int i = 0; i < currentRoute.length; i++)
    {
        System.out.println(currentRoute[i]);
    }

there are a lot more switch statements, but I have just included 2 for this example.

EDIT:both switch and for statements are located within the same method.

3
  • You've done a bit of creative code formatting, however please understand that code that is formatted in a boring standard way is much easier to read and understand. Commented Apr 26, 2014 at 2:22
  • Why do you want to declare the array within the switch statement? Commented Apr 26, 2014 at 2:32
  • Short answer: No. A variable can not escape the scope it was declared in. Commented Apr 26, 2014 at 2:35

2 Answers 2

7

Do it like this

String[] currentRoute = null;
switch (routeID)
{
    case "1" : {
        currentRoute = Route1;
    }
    case "96" : {
        currentRoute = Route96;
    }
}

if (currentRoute != null )
    // print out values in currentRoute
    for (int i = 0; i < currentRoute.length; i++)
    {
       System.out.println(currentRoute[i]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

6

Your case labels define an inner local scope by using curly braces. A general rule in Java is that you cannot access variables from an inner scope when you are in the outer scope (however, variables from the outer scope defined before the inner scope remain visible). Therefore, there is no way to access currentRoute defined inside the switch.

The solution is to define currentRoute outside the switch, do the assignment inside the switch, and keep accessing the variable after the switch is over:

String[] currentRoute;
switch (routeID) {
    case "1" : {
        currentRoute = Route1;
    }
    case "96" : {
        currentRoute = Route96;
    }
    default:
        currentRoute = new String[0];
}

Note that your code also had a redundancy - in both declarations of the inner currentRoute you assigned it a new array, and then immediately discarded that value.

Also note that I added a default. Without a default, Java compiler would complain about currentRoute not being initialized.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.