Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot resolve method 'toString(void)'

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading