0
import java.util.ArrayList;

public class FizzBuzz{

    public static void main(String[] args){
        System.out.println(fizzBuzz(15));
    }

    public ArrayList<String> fizzBuzz(int n) {
        ArrayList<String> results = new ArrayList<String>();
        for (int i = 1; i <= n; i++) {
            if (i % 15 == 0) {
                results.add("fizz buzz");
            } else if (i % 5 == 0) {
                results.add("buzz");
            } else if (i % 3 == 0) {
                results.add("fizz");
            } else {
                results.add(String.valueOf(i));
            }
        }
        return results;
    }
}

I am trying to print out the FizzBuzz array. however, why I can't call the method below by the name here? could anyone help me out please? Appreciate!

dz99194n
  • 19
  • 4
  • 4
    Possible duplicate of [Non-static variable cannot be referenced from a static context](https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – Timothy Truckle Jul 23 '17 at 22:46
  • 1
    Possible duplicate of [Does every method in main class have to be static?](https://stackoverflow.com/questions/8117781/does-every-method-in-main-class-have-to-be-static) – tima Jul 23 '17 at 23:33

2 Answers2

0

You could instantiate an object first and then call the method on it. Something like this:

import java.util.ArrayList;

public class FizzBuzz{

    public static void main(String[] args){
        FizzBuzz obj = new FizzBuzz; //Create an instance
        System.out.println(obj.fizzBuzz(15)); //Call the mouthed through that
    }

    public ArrayList<String> fizzBuzz(int n) {
        ArrayList<String> results = new ArrayList<String>();
        for (int i = 1; i <= n; i++) {
            if (i % 15 == 0) {
                results.add("fizz buzz");
            } else if (i % 5 == 0) {
                results.add("buzz");
            } else if (i % 3 == 0) {
                results.add("fizz");
            } else {
                results.add(String.valueOf(i));
            }
        }
        return results;
    }
}

An easier way would be to just change the fizzbuzz() method to static but that is not a good idea

Now you will get your desired output using either of the methods described above

[1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizz buzz]
anon
  • 1,258
  • 10
  • 17
0

One way around this problem is to just change your method to static:

public static ArrayList<String> fizzBuzz(int n) {
    //...

If you call a method from the main method it must be static because the main method is static, and static methods can only reference static objects.


But the better practice is to make an instance of your class, then call the method through the instance of your class:

public static void main(String[] args){
    FizzBuzz fb = new FizzBuzz();
    ArrayList<String> result = fb.fizzBuzz(15);
    System.out.println(result);
}

That way you don't have to make the method static.


Either way you get the same output, but the second option is more professional, neater, and altogether a better practice.