0

So, I have a problem but I do not really know what exactly is causing it. I had a program that inicially worked, but it couldn't afford every entry. In order to fix this I added a try - catch block. Before the change, I only had the two lines you can see inside the try block, instead of all the try - catch code. Until now, I think everything should be right. But as I tried to compile my code, I got an ""error: cannot find symbol (variable: workedPer)"". I thought the try block was always executed, so why is that variable not being defined? I have looked into other similar questions, but couldn't find a solution.

NOTE: This is a portion of the code, I only put this in order to make the problem easier to see. But if you need more code please let me know.

try
{
    String[] workedPer = newPer.split("=");
    workedPer[1] = workedPer[1].substring(0, workedPer[1].length() -1);
}

catch (ArrayIndexOutOfBoundsException ex)
{
    System.out.println("Invalid Entry. Program will stop now...");
    System.exit(1);
}

for (Material mat : readyContent)
{
    if ((mat.category).equals(workedPer[0]))
    {
        checker = true;
    }
}
Charles
  • 25
  • 5

1 Answers1

1

Because, scope of workedPer variable is limited to try block.

You need to change your code to

String[] workedPer = null;
try
{
    workedPer = newPer.split("=");
    workedPer[1] = workedPer[1].substring(0, workedPer[1].length() -1);
}

So that, it can be accessible inside for loop

for (Material mat : readyContent)
{
    if ((mat.category).equals(workedPer[0])) // you are using it here
    {
        checker = true;
    }
}
Ravi
  • 30,829
  • 42
  • 119
  • 173