-1

I'm trying to mimic a solution to the FizzBuzz problem in Eclipse. The solution class has been given, but I'm not entirely sure on how to run it in main to print the result. In the solution, the list goes up to 15 and prints out the results. If I run it like this, is the list created for s in main? and if so, how do I print it as a list instead of getting "Solution@7852e922" object output?

 public class FizzBuzzMain {

     public static void main(String[] args) {
     Solution s = new Solution();
     System.out.println(s);

     }
 }


  import java.util.ArrayList;
  import java.util.List;

 public class Solution {
     public List<String> fizzBuzz(int n) {
         List<String> list = new ArrayList<>();
         for(int i = 1;i<=n;i++){
             if(i%3==0&&i%5==0){
                 list.add("FizzBuzz");
             }
             else if (i%3==0) list.add("Fizz");
             else if(i%5==0) list.add("Buzz");
             else{
                 list.add(Integer.toString(i));
             }
         }
         return list;
     }
 }
jimmyg8085
  • 13
  • 2
  • You need to actually call your method and print the list it returns, not the class itself. – azurefrog May 16 '19 at 19:23
  • @azurefrog how would I go about calling it in main? I thought I had to create an object instance of the class to run it. Sorry I know the solution class given is correct, I'm just trying to figure out how to run it in main. That part wasn't given and I've been trying everything I can think of. – jimmyg8085 May 16 '19 at 19:29
  • You would need to use `List list = s.fizzBuzz(intValue);` then use a loop to print out each value of the list. – Nexevis May 16 '19 at 19:31

2 Answers2

2

In your main method you need to just call the fizzBuzz() method of the newly created Solution object and loop through the results:

 public static void main(String[] args) {
     Solution s = new Solution();
     List<String> result = s.fizzBuzz(100);
     for (int n : result) {
         System.out.println(n);
     }
 }
ruohola
  • 21,987
  • 6
  • 62
  • 97
1

You can't run a class, you can only run a method. I assume you want to run the fizzBuzz(int n) method of the Solution class. You do that by calling it, e.g.

 List<String> fizz = s.fizzBuzz(15);
Aloso
  • 5,123
  • 4
  • 24
  • 41