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!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
John E.
  • 321
  • 4
  • 11
  • 1
    Java arrays don't override `Object.toString()`. You can use `Arrays.toString()`. – Elliott Frisch Sep 16 '16 at 04:06
  • @ElliottFrisch I've already tried it, still gave me the same output – John E. Sep 16 '16 at 04:07
  • 1
    @S.Anthony Are you sure? `Arrays.toString(anArray);` should work just fine. – Andrew Li Sep 16 '16 at 04:10
  • @AndrewL. One second, I will add this code too – John E. Sep 16 '16 at 04:10
  • Please don't edit you question to add things like "(solved)" in the title, and invalidate answers by correcting your code. If you managed to solve your own problem, post an answer with your solution (or accept the answer that helped you solved the problem). I have reverted your last edit. – Mark Rotteveel Sep 16 '16 at 10:25

2 Answers2

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.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
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....

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97