Manfredi
Manfredi

Reputation: 172

Initialising variables inside for loop

I have a for loop, and I initialise the variables inside that loop. After it, I need to manipulate that variables, but the compiler says that variables are not initialised.

public class Solution {
    static void displayPathtoPrincess(int n, String[] grid) {
        String[][] visual = new String[n][n];
        for (int i = 0; i < n; i++) {
            char[] myGrid = grid[i].toCharArray();
            for (int j = 0; j < n; j++) {
                visual[i][j] = myGrid[j] + "";
            }
        }

        int pX;
        int pY;
        int bX;
        int bY;
        // rescue the princess
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (visual[i][j].equals("p")) {
                    pX = j;
                    pY = i;
                }
                if (visual[i][j].equals("m")) {
                    bX = j;
                    bY = i;
                }
            }
        }
        System.out.println(pY + "");
        System.out.println(pX + "");
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m;
        m = in.nextInt();
        String grid[] = new String[m];
        for (int i = 0; i < m; i++) {
            grid[i] = in.next();
        }

        displayPathtoPrincess(m, grid);
    }
}

if condition is true at some point. In fact if I move the print statements inside the if it works.
How can solve this problem?

Upvotes: 1

Views: 663

Answers (4)

D.Urvoy
D.Urvoy

Reputation: 19

If you want to initialize your variables in conditions, then you need to set them to a default value :

int pX = 0;
int pY = 0;
int bX = 0;
int bY = 0;

That way, your compiler will not return the initialization issue any more.

EDIT

I made some modifications, since I could not make your code work (cast from string to int, array of char initialization issue, etc.). Could you try this please :

public class Solution {
static void displayPathtoPrincess(String grid) {
    int n = grid.length();
    String[][] visual = new String[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            visual[i][j] = String.valueOf(grid.charAt(j));
        }
    }

    int pX = 0;
    int pY = 0;
    int bX;
    int bY;
    // rescue the princess
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (visual[i][j].equals("p")) {
                pX = j;
                pY = i;
            }
            if (visual[i][j].equals("m")) {
                bX = j;
                bY = i;
            }
        }
    }
    System.out.println(pY + "");
    System.out.println(pX + "");
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String grid = in.next();

    displayPathtoPrincess(grid);
}

It probably isn't what you want to do, but I have no problem with your variables' values initialized in conditions.

Upvotes: 1

hagrawal7777
hagrawal7777

Reputation: 14658

Below excerpt from JLS §16 (Definite Assignment)

For every access of a local variable or blank final field x, x must be definitely assigned before the access, or a compile-time error occurs.

If you using a local variable at any point in your method, and compiler is not sure that local variable would be initialized when actually running the program then compiler will show error that please initialize local variable.

In you case, if n is 0 then your for (int i = 0; i < n; i++) { loop will never run, so compiler cannot be sure that at this point System.out.println(pY + "");, pY would be in initialized state, so complains.

For solution: You must initialize local variable before using/reading it.

You are able to print pX and pY because compiler is sure at this point that values would be initialized, however try below and even in your IF block you will see that there is compilation error because you would be trying to read the local variables before they were initialized, so compiler complains.

So, bottom line - make compiler believe that local variable would have been initialized at that point, so initialize local variables before using them.

if (visual[i][j].equals("p")) {
                    System.out.println(pY + "");  //Compilation error because compiler is not sure if LV would be in initialized state.
                    pX = j;
                    pY = i;
                    System.out.println(pY + "");  //Happy compiler becuse it is sure that LV are just initialized.
                }

Upvotes: 0

Dan Heidinga
Dan Heidinga

Reputation: 489

The compiler needs to see that the variables have been definitely assigned regardless of which path through the loop the execution actually takes at runtime.

It doesn't know if either of the if branches will run when your program executes.

It is a good practice to always give your variables an initial value to avoid these kind of issues.

Upvotes: 0

ABHISHEK RANA
ABHISHEK RANA

Reputation: 327

you could try this

class Demo
{
static int pX;
static int pY;
static int bX;
static int bY;

public static void main(String a)
{
for (int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
        if (visual[i][j].equals("p")) {
            pX = j;
            pY = i;
        }
        if (visual[i][j].equals("m")) {
            bX = j;
            bY = i;
        }
    }
}
System.out.println(pY + "");
System.out.println(pX + ""); 
}

}

Upvotes: 0

Related Questions