Sackling
Sackling

Reputation: 1820

Printing int[] array from a display method

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

Upvotes: 0

Views: 149

Answers (6)

Prine
Prine

Reputation: 12528

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..

Upvotes: 1

Mayur
Mayur

Reputation: 676

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]);
            }
    }   
}

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

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]);
            }
    }   
}

Upvotes: 0

Tom
Tom

Reputation: 4180

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]);
        }
}   
}

Upvotes: 0

scibuff
scibuff

Reputation: 13755

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

private int[] intArray;

public LargeInteger(String s){

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

}

Upvotes: 0

SLaks
SLaks

Reputation: 887415

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

You need to make a private field instead.

Upvotes: 0

Related Questions