import java.util.Arrays;
public class Fizzi {
public static void main(String[] args) {
String [] arr = {"1, 2, 3, 5, 35, 40"};
System.out.println("Boolean Array: " + Arrays.toString(FizziRun(arr)));
}
public static void FizziRun(String[] arr) {
int i;
for (i = 0; i <= arr.length - 1; i++) {
if (i % 3 == 0) {
System.out.println("Fizz");
} else if (i % 5 == 0) {
System.out.println("Buzz");
} else if ((i % 3 & i % 5) == 0) {
System.out.println("FizzBuzZ");
} else {
System.out.println("" + i);
}
}
}
}
Using this code I have the message: "Cannot resolve method ‘toString(void)’" . How to avoid this and please explain the solution.
>Solution :
the toString() method is inherited from every class that extends Object (which means, all the classes) and the standard use is to call it as an instance method. It is very common to override the toString() method from the object class for a more human readable way, which usually replaces the class name + hashcode for a list of attributes and their values.
In your case, you’re passing an argument to the toString() method that does not expected an argument. Because of that, java compiler is saying you cannot pass a void return to a method that expects something else.
Moreover, the argument passed is another function that already prints out whatever you need. In that case, you just need to call FizziRun(arr) to see the output to the console.
If you work with an array of primitives, your approach is ok. If you decide to move to a collection of objects, override the toString method inside that class