java printing an array with Arrays.toString() error

14,595

Solution 1

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

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

Solution 3

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.

Share:
14,595

Related videos on Youtube

Levon Tamrazov
Author by

Levon Tamrazov

Updated on September 26, 2022

Comments

  • Levon Tamrazov
    Levon Tamrazov over 1 year

    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!

  • pb2q
    pb2q over 11 years
    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.
  • Sashi Kant
    Sashi Kant over 11 years
    If you try this for arrays: it will print the comma separated data
  • Lo Juego
    Lo Juego over 11 years
    Maybe you have a variable named Arrays? Common! My answer was downvoter for less :)
  • Levon Tamrazov
    Levon Tamrazov over 11 years
    Yeah I tried this as well before posting this question, it printed the memory address.
  • Levon Tamrazov
    Levon Tamrazov over 11 years
    As I just commented above I called the class for this program Arrays, so that's probably it. Thank you for your feedback!
  • Levon Tamrazov
    Levon Tamrazov over 11 years
    Also thank you for that workaround! I used it instead of having to rename the class. Worked like a charm!