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!

3
  • looks fine to me. Test with an array like this: int[] totals = { 1, 2, 3, 4 }; Commented Oct 16, 2012 at 14:38
  • are you getting any other compiler errors or warnings? Commented Oct 16, 2012 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! Commented Oct 16, 2012 at 15:45

5 Answers 5

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)); 
Sign up to request clarification or add additional context in comments.

3 Comments

Maybe you have a variable named Arrays? Common! My answer was downvoter for less :)
As I just commented above I called the class for this program Arrays, so that's probably it. Thank you for your feedback!
Also thank you for that workaround! I used it instead of having to rename the class. Worked like a charm!
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.

2 Comments

or rather: he's got a variable named Arrays: Object Arrays = null
I called the class for this program Arrays. That must be it, so I'll just rename the class. Thank you!
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.

Comments

-1

Why dont you try ::

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

3 Comments

toString() on an array won't do anything useful: it just prints the reference for the array. The OP wants to use the Arrays class to print each value.
If you try this for arrays: it will print the comma separated data
Yeah I tried this as well before posting this question, it printed the memory address.
-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));

1 Comment

If the array hadn't been initialised, I wouldn't expect the argument exception.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.