1

I am trying to print array's content with

toString()

and I can't figure out what I am doing wrong.

The output should be 5 random numbers from 0 to 100 which I will store in array after all I have to print them all.

Here is my code:

public class Ary {

private int[] anArray;
private int arraySize;
private String numberAsString;
Random r = new Random();

public Ary(int arraySize) {
    this.anArray = printArray();
}

public Ary() {
    arraySize = 2;
    printArray();
}

public int getArraySize() {
    return arraySize;
}

public void setArraySize(int arraySize) {
    this.arraySize = arraySize;
}

public int[] printArray() {
    // Assign anArray with a custom number
    anArray = new int[arraySize];

    for(int numbers : anArray) {
        anArray[numbers] = r.nextInt(100);
        System.out.println(anArray[numbers] + " ");
    }
    return anArray;
}

@Override
public String toString() {
    return "Array = " + Arrays.toString(anArray);
}
}

Output:

Music.app.Ary@5e2de80c

Here is my code with Arrays.toString():

public int[] printArray() {
    // Assign anArray with a custom number
    anArray = new int[arraySize];

    for(int numbers : anArray) {
        anArray[numbers] = r.nextInt(100);
        System.out.println(Arrays.toString(anArray));
    }
    return anArray;
}

I've already tried tons of methods but still haven't figure it out.. Can you please explain what am I doing wrong?

Thank you very much!

4
  • 1
    Java arrays don't override Object.toString(). You can use Arrays.toString(). Commented Sep 16, 2016 at 4:06
  • @ElliottFrisch I've already tried it, still gave me the same output Commented Sep 16, 2016 at 4:07
  • 1
    @S.Anthony Are you sure? Arrays.toString(anArray); should work just fine. Commented Sep 16, 2016 at 4:10
  • @AndrewL. One second, I will add this code too Commented Sep 16, 2016 at 4:10

2 Answers 2

2

You've changed the signature of toString() (so you aren't calling the method you have defined). Instead, you need something like1,

@Override
public String toString() {
    return "Array = " + Arrays.toString(anArray);
}

And you should probably initialize anArray in your constructor(s)2 and remove "printArray"

public Ary(int arraySize) {
    this.arraySize = arraySize;
    this.anArray = new int[arraySize];
    for (int i = 0; i < arraySize; i++) {
        this.anArray[i] = r.nextInt(101); // <-- [0, 100], or [0, 101)
    }
}

public Ary() {
    this(5); // <-- use the other constructor.
}

1And the override annotation will alert you to this mistake.
2And you shouldn't print the array until you finish initializing it.

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

4 Comments

Hm...interesting. Now I am getting this: Array = [ ]
@S.Anthony And where / how are you initializing the array now?
I updated my code
Found my mistake, works well. Thank you very much Elliott
1

This is a custom method System.out.print(array.toString()); so you overloaded the toString() BUT for printing out the object in the console is the toString() method (no params) what is getting invoked...

remove the int[] parameter and it will called normally, you need for sure modify the body of the method too

public String toString() {
    return "Array = " + Arrays.toString(a);
}

ussing teh override annotation will be recommended always, it will prevent that errors while writing the code....

2 Comments

You might want the @Override annotation there to override Object's method
Also, it isn't clear where a came from here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.