How Do You Troubleshoot “error: no suitable method found for toString(String)”?

Problem scenario
You are trying to print out a multi-dimensional array in Java. When you try to compile your program, you get this:

error: no suitable method found for toString(String)
System.out.println(Arrays.toString(Arrays.toString(var1)));
^
method Arrays.toString(long[]) is not applicable
(argument mismatch; String cannot be converted to long[])

What should you do?

Solution
You need to pass a one-dimensional array to Arrays.toString in the System.out.println statement.

Are you using Arrays.toString in a nested way?

Assuming you have a two-dimensional array called "arr2d_var1", you may have tried this:

System.out.println(Arrays.toString(Arrays.toString(arr2d_var1)));

Instead of using Arrays.toString in a nested way, make sure you have a single-dimensional array as the argument for Arrays.toString. Here is a corrected example:

System.out.println(Arrays.toString(arr2d_var1[0]));

Leave a comment

Your email address will not be published. Required fields are marked *