3

So I was trying to print an array of ints in my program, and following these instructions What's the simplest way to print a Java array?

wrote the following code:

    int[] totals = //method that returns int array
    System.out.println(Arrays.toString(totals));

But it wont compile saying that

"method toString in class Object cannot be applied to given types. required: no arguments found: int[] reason: actual and formal argument list differ in length"

Why does it do that? Do I not pass my array as an argument to toString? If not, how do I use toString?

Thank you!

Community
  • 1
  • 1
Levon Tamrazov
  • 530
  • 2
  • 9
  • 20
  • looks fine to me. Test with an array like this: `int[] totals = { 1, 2, 3, 4 };` – pb2q Oct 16 '12 at 14:38
  • are you getting any other compiler errors or warnings? – jalynn2 Oct 16 '12 at 14:43
  • No, I wasn't getting any other errors. But the two answers bellow helped me out. My main class was named Arrays (neubie mistake), so it was confused what I was referencing when I wrote Arrays.toString(totals). Thanks for the feedback! – Levon Tamrazov Oct 16 '12 at 15:45

5 Answers5

14

method toString in class Object cannot be applied to given types. required: no arguments found: int[] reason: actual and formal argument list differ in length

May be you have a variable named Arrays, that's why the compiler is complaining thinking that you are trying to invoke the Object.toString(), which doesn't take any argument.

Try

 System.out.println(java.util.Arrays.toString(totals)); 
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
2

The fact that it says "method toString in class Object cannot be applied to given types." makes me think you might not be importing the java.util.Arrays class properly, or you have some other object called Arrays.

Omaha
  • 2,262
  • 15
  • 18
1

This works for me:

int[] totals = {1,2};
System.out.println(Arrays.toString(totals));

printing:

[1, 2]

Are you sure you use at least JDK 5 ?

Indeed, Arrays.toString(int[] a) only exists since JDK 5.

Mik378
  • 21,881
  • 15
  • 82
  • 180
-1

Why dont you try ::

int[] totals = //method that returns int array
    System.out.println(totals.toString());
Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
-1

Your method doesn't seem to init your array, try this:

int[] totals = new int[10]; //method that returns int array
System.out.println(Arrays.toString(totals));
Lo Juego
  • 1,305
  • 11
  • 12
  • If the array hadn't been initialised, I wouldn't expect the argument exception. – Edd Oct 16 '12 at 15:31