1

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 ) {
                .....
Raedwald
  • 46,613
  • 43
  • 151
  • 237

4 Answers4

1

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 ) {
Norbert
  • 6,026
  • 3
  • 17
  • 40
1

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);
laune
  • 31,114
  • 3
  • 29
  • 42
1

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

Drew Kennedy
  • 4,118
  • 4
  • 24
  • 34
0

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

Solution: initialize them before the conditions.

  • "declare them before the conditions", not "initialize them before the conditions". It's not an initialization issue; the compiler doesn't recognize that they've been declared. – Erick G. Hagstrom Feb 26 '16 at 20:30