wendy ouyang
wendy ouyang

Reputation: 13

"cannot find symbol: variable" despite declaring the variable

When I compiled the code below, it said:

"error: cannot find symbol: variable max"

But I did define the variable max....

public class Solution {
    public static  boolean isOneEditDistance(String s, String t) {
            if (s.length() >= t.length() ) {
                    StringBuilder a = new StringBuilder (s);
                    StringBuilder b = new StringBuilder (t);
                    int max = s.length();
                    int min = t.length();
            }
            else {
                    StringBuilder a = new StringBuilder (t);
                    StringBuilder b = new StringBuilder (s);
                    int max = t.length();
                    int min = s.length();
            }

            int flag = 0;

            if ( (max-min)>1 )
                    return flase;
            else if ( (max-min)==1 ) {
                .....

Upvotes: 1

Views: 1625

Answers (4)

Drew Kennedy
Drew Kennedy

Reputation: 4168

The reason the variable can't be seen is due to the scope of the variable. Variables are able to "drill down" the visibility chain, but not "dig out." Because the variable was declared in an if-statemnent, your variable is only visible within that if-statement because it cannot "dig out."

Here's my awesome MS Paint skills to poorly depict what I mean about not being able to "dig out" of scope visibility:

enter image description here

Upvotes: 1

Anuraj Kathait
Anuraj Kathait

Reputation: 23

variables have been initialized inif (s.length() >= t.length() ) block. Therefore those variables are limited to it.

Solution: initialize them before the conditions.

Upvotes: 0

laune
laune

Reputation: 31290

I would prefer the code as shown below, which also avoids issues due to a and b being scoped in the blocks contained in the if statement.

 if( s.length() < t.length() ){
     String h = s; s = t; t = h;
 }
 // now s is not shorter than t
 int max = s.length();
 int min = t.length();
 StringBuilder a = new StringBuilder(s);
 StringBuilder b = new StringBuilder(t);

Upvotes: 1

Norbert
Norbert

Reputation: 6084

You have the scope of the variables/parameters incorrect:

If a parameter is within { } the scope is limited to within these { }

So change your code to this to change the scope and solve the issue:

public class Solution {
public static  boolean isOneEditDistance(String s, String t) {
int max=0;
int min=0;
        if (s.length() >= t.length() ) {
                StringBuilder a = new StringBuilder (s);
                StringBuilder b = new StringBuilder (t);
                max = s.length();
                min = t.length();
        }
        else {
                StringBuilder a = new StringBuilder (t);
                StringBuilder b = new StringBuilder (s);
                max = t.length();
                min = s.length();
        }

        int flag = 0;

        if ( (max-min)>1 )
                return flase;
        else if ( (max-min)==1 ) {

Upvotes: 1

Related Questions