in4no
in4no

Reputation: 11

How the java variable scope is working here?

I was trying out a sample problem statement and correct code for which is something like this -

import java.util.Scanner;

public class Sample {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int m = 5, n = 5;
        int dist = 0;
        for(int i = 0; i < m; ++i) {
            for(int j = 0; j < n; ++j) {
                int value = scanner.nextInt();
                if(value == 1) {
                    dist = Math.abs(i - 2) + Math.abs(j - 2);
                }
            }
            scanner.nextLine();
        }
        System.out.println(dist);
    }
}

This runs perfectly giving the correct answer. But when I write the code -

import java.util.Scanner;

public class Sample {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int m = 5, n = 5;
        int i = 0, j = 0;
        int dist = 0;
        for(; i < m; ++i) {
            for(; j < n; ++j) {
                int value = scanner.nextInt();
                if(value == 1) {
                    dist = Math.abs(i - 2) + Math.abs(j - 2);
                }
            }
            scanner.nextLine();
        }
        System.out.println(dist);
    }
}

The answer is 0, always. For the sample input -

0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

The correct answer is 3 and I am getting 3 while running the 1st piece of code. But not while running the 2nd piece of code.

Upvotes: 1

Views: 64

Answers (3)

fjlopez
fjlopez

Reputation: 21

Another approach could be the following to reset the variable j as mentioned previously

public class Sample {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int m = 5, n = 5;
        int i = 0, j = 0;
        int dist = 0;
        for(; i < m; ++i, j = 0) {
            for(; j < n; ++j) {
                int value = scanner.nextInt();
                if(value == 1) {
                    dist = Math.abs(i - 2) + Math.abs(j - 2);
                }
            }
            scanner.nextLine();
        }
        System.out.println(dist);
    }
}

Upvotes: 1

NickChris
NickChris

Reputation: 71

In the first piece of code, the variable j is reset back to 0 each time the code exits and restarts that inner loop. In the second piece of code, that does not happen. A step by step look at what the variables are storing might be helpful. I added the following line inside the inner loop and commented out the user input section for debugging purposes:

System.out.println("( " + i + " " + j + ")");

With the first example, this prints:

( 0 0)
( 0 1)
( 0 2)
( 0 3)
( 0 4)
( 1 0)
( 1 1)
( 1 2)
( 1 3)
( 1 4)
( 2 0)
( 2 1)
( 2 2)
( 2 3)
( 2 4)
( 3 0)
( 3 1)
( 3 2)
( 3 3)
( 3 4)
( 4 0)
( 4 1)
( 4 2)
( 4 3)
( 4 4)

However for the second example it prints:

( 0 0)
( 0 1)
( 0 2)
( 0 3)
( 0 4)

Essentially in the second example, j is not reset for each iteration of the outer loop. If you add more print statements in other places, for instance inside the outer loop but not the inner one, the scoping will become even more clear. It's important to note that the outer loop is still running 5 times, we just have no evidence of that as the print statement becomes unreachable after the first pass through the inner loop.

Upvotes: 0

Edgar Domingues
Edgar Domingues

Reputation: 990

The problem is that the variable j is not reset to 0.

The correct code would be:

import java.util.Scanner;

public class Sample {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int m = 5, n = 5;
        int i = 0;
        int dist = 0;
        for(; i < m; ++i) {
            int j = 0; // <== this line was missing
            for(; j < n; ++j) {
                int value = scanner.nextInt();
                if(value == 1) {
                    dist = Math.abs(i - 2) + Math.abs(j - 2);
                }
            }
            scanner.nextLine();
        }
        System.out.println(dist);
    }
}

If you don't know, see how the for statement works in https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

Upvotes: 0

Related Questions