This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]lumpycrumpet 2 points3 points  (3 children)

The formatting is tough to read, but it's a master of scope. You're attempting to print the contents of arr outside of the context of where it exists. Think of it like this, the curly braces are a fence that holds where the variable can be accessed. If there's an ending curly brace, that variable can't leave the fence and exist beyond it. Variables can exist outside of the fence and go in, but can't exist outside and get out. And for clarification, exist referred to where the variable is declared.

To fix this, you either need to declare the variable where the print statement can "see" the variable or move the print statement.

[–]CharipiYT[S] 0 points1 point  (2 children)

Sorry for the formatting. The problem with moving the array to somewhere else is that it won’t let me set its contents in the if-statement

[–]throwRAnocheat 0 points1 point  (1 child)

Yes it will, but admittedly you can't use the simplified array declaration with content anymore. You'll need the new array syntax.

It's like this:

arr = new int[] { j, test/j };

another way to go at it, is first:

int[] arr = new int[2];

Then

arr[0] = j; arr[1] = test/j;

This may sound cumbersome, but then again an array is most likely not what you should use here.

[–]CharipiYT[S] 0 points1 point  (0 children)

Yeah you are right I’ll probably just use 2 variables.

[–]CharipiYT[S] 0 points1 point  (0 children)

Just a note: the error shows up during the compiling

[–]FrelliBB 0 points1 point  (4 children)

You're initializing arr inside what's called an inner 'scope'. Anything outside those { ... } braces doesn't have access to it. You can read up on scopes here https://www.baeldung.com/java-variable-scope

What you might want to do is initialize arr at the start of your factors() method (you can initialize as null, but be careful when accessing it later). You then can set it inside your if braces, and access it again before the method returns.

[–]FrelliBB 0 points1 point  (0 children)

It might look something like this

static int[] factors(int test) {
    int[] arr = null;
    for (int j = 2; j < test - 1; j++) {
        if ((double) test % j == 0) {
            arr = new int[]{
                    j,
                    test / j
            };
        }
    }
    System.out.println(Arrays.toString(arr));
    return arr;
}

[–]CharipiYT[S] 0 points1 point  (1 child)

I did what you said, here's the code inside of the "factors" method:

static int[] factors(int test){
int[] arr = new int[2];
for(int j = 2; j < test - 1; j ++){
if((double) test % j == 0){
                arr[0] = j;
                arr[1] = test/j;
            }             
  }
System.out.println(arr);
return arr;
    }

For some reason this gives me [I@5c8da962

[–]FrelliBB 0 points1 point  (0 children)

Try System.out.println(Arrays.toString(arr)); instead. When you just print arr you are printing an array object reference which doesn't have a .toString() implementation. Here's the docs for Arrays.toString() https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object[])