0

I am trying to print an int[] array from a seperate method in the same class.

public class LargeInteger {

    public LargeInteger(String s) {

        int[] intArray = new int[s.length()];

        for (int i = 0; i < s.length(); i++) {
            intArray[i] = Character.digit(s.charAt(i), 10);
        }
    }

    public Object display() {

         for (int i = 0; i < intArray.length; i++) {     
                System.out.print(intArray[i]);
            }
    }   
}

My intArray is clearly being hidden from the display method, but I am not sure what to do

6 Answers 6

1

I will give you the answer but you should first invest some time to look up your problem on google. Google knows "almost" everything...

public class LargeInteger {

    private int[] intArray;

    public LargeInteger(String s) {

        intArray = new int[s.length()];

        for (int i = 0; i < s.length(); i++) {
            intArray[i] = Character.digit(s.charAt(i), 10);
        }
    }

    public void display() {

         for (int i = 0; i < intArray.length; i++) {     
                System.out.print(intArray[i]);
            }
    }   
}

And your display method should be void if it isn't returning anything..

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I tried googling of course. It is very hard to find specifics like this though. I mean I can easily find how to loop an array for instance but finding out how to split the display into another method is harder to find.
0

intArray is a local variable in the constructor.
It doesn't exist anywhere else.

You need to make a private field instead.

Comments

0

You need to declare the array outside of LargeInteger method, eg

private int[] intArray;

public LargeInteger(String s){

    this.intArray = new int[s.length()];

}

Comments

0
public class LargeInteger {

private int[] intArray;

public LargeInteger(String s) {

    this.intArray = new int[s.length()];

    for (int i = 0; i < s.length(); i++) {
        intArray[i] = Character.digit(s.charAt(i), 10);
    }
}

public Object display() {

     for (int i = 0; i < this.intArray.length; i++) {     
            System.out.print(intArray[i]);
        }
}   
}

Comments

0

Make intArray a member of the LargeInteger class instead of a local to the constructor:

public class LargeInteger {

    private int[] intArray;

    public LargeInteger(String s) {

        intArray = new int[s.length()];

        for (int i = 0; i < s.length(); i++) {
            intArray[i] = Character.digit(s.charAt(i), 10);
        }
    }

    public Object display() {

         for (int i = 0; i < intArray.length; i++) {     
                System.out.print(intArray[i]);
            }
    }   
}

Comments

0

Just declare int[] intArray out of the constructor.

It should be

public class LargeInteger {

    private int[] intArray;

    public LargeInteger(String s) {

        intArray = new int[s.length()];

        for (int i = 0; i < s.length(); i++) {
            intArray[i] = Character.digit(s.charAt(i), 10);
        }
    }

    public Object display() {

         for (int i = 0; i < intArray.length; i++) {     
                System.out.print(intArray[i]);
            }
    }   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.