-2

I have been given the classic FizzBuzz test to do. However, the problem has an extra task which is to display the results in an ArrayList. I know that you can't just stick an integer and string values together in a list. Hence, this is causing problems for me.

I have made an attempt on converting the numbers gotten from the loop to string however, it became too complicated and I thought maybe I was over-complicating things. Or maybe I am doing something wrong for an easy task such as this to be so time-consuming.

Below I have included the most recent code I have tried and the guide comments I was given.

  1. Implement the loop that will take the List Range and parse for the values to solve FizzBuzz.
  2. You will need to add a successfully value within the loop to the Private Class variable 'private List range'.
  3. As an example, while looping 3 will equal fizz which should be added to the fizzBuzzList.
  4. The two private variables have been set below for you to hold both the list and the FizzBuzz List.

    public class FizzBuzz {
    
    private List range;
    private List fizzBuzzList = new ArrayList();
    
    
    public FizzBuzz(int startOfRange, int endOfRange){
        range = IntStream.rangeClosed(startOfRange, endOfRange).boxed().collect(Collectors.toList());
        fizzBuzzIterator(range);
    }
    
    public void fizzBuzzIterator(List range){
    
        for (int i = 1 ; i < range.size(); i++) {
    
            if (((i % 3) == 0) && ((i % 5) == 0)) {
                fizzBuzzList.add("FizzBuzz");
            }
    
            else if ((i % 3) == 0) {
    
                fizzBuzzList.add("Fizz");
            }
    
            else if ((i % 5) == 0) {
                fizzBuzzList.add("Buzz");
            }
    
            else {
                fizzBuzzList.add(i);
            }
    
        }
    }
    
    
    
    public List getFizzBuzzList() {
        return fizzBuzzList;
    }
    
    
     }
    

Below is the test code which I am supposed to test the results on.

public class FizzbuzzTests {

    FizzBuzz fizzBuzz = new FizzBuzz(1,16);

    @Test
    public void fizzBuzzIteratorTest(){
        List<String> minFizzBuzzList = List.of("1","2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz");
        Assert.assertEquals(fizzBuzz.getFizzBuzzList(), minFizzBuzzList);
    }

}

The expected results as I am shown is supposed to be in the format such as [1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz]. The code written above gives me the results in the same format, however, the difference is that one utilizes "java.util.ArrayList" while other utilizes "java.util.ImmutableCollections$ListN" and thus this gives me an error.

I have been stuck trying to fix this for a while now and I am running out of ideas to try. Any advice would be much helpful and appreciated. Thanks.

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
G_Li
  • 1
  • 1
  • 4
    `Integer.toString(num);`. Alternatively, use Object as the generic type for the list. Either way, avoid the use of raw types – Zoe Aug 02 '19 at 18:38
  • https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – achAmháin Aug 02 '19 at 18:40
  • Why wouldn't you just use `List` and store the numbers as a `String`. It is not like you need to actually use the numbers to do calculations. – Nexevis Aug 02 '19 at 18:45
  • Just a hint. Something divisible by both 3 and 5 is divisible by 15. – WJS Aug 03 '19 at 01:03

3 Answers3

1

This solution converts the ints to a string using Integer.toString(int x) function.

List<String> list = IntStream.rangeClosed(startOfRange, endOfRange)
            .mapToObj(this::convert)
            .collect(Collectors.toList());
System.out.println(list);

private String convert(int number) {
    if (((number % 3) == 0) && ((number % 5) == 0)) {
        return "FizzBuzz";
    } else if ((number % 3) == 0) {
        return "Fizz";
    } else if ((number % 5) == 0) {
        return "Buzz";
    } else {
        return Integer.toString(number);
    }
}
Matt Berteaux
  • 763
  • 4
  • 12
0

I would store all the values into a List<String> rather than a generic and take advantage of Integer.toString() to convert the iterator into a String to store the value.

The FizzBuzz class would look like this in my version:

private List<String> fizzBuzzList;

public FizzBuzz(int start, int end){
    fizzBuzzList = new ArrayList<>();
    this.populateFizzBuzz(start, end);
}

private void populateFizzBuzz(int start, int end){
    for (int i = start ; i < end; i++){
        if (((i % 3) == 0) && ((i % 5) == 0)) {
            fizzBuzzList.add("FizzBuzz");
        }
        else if ((i % 3) == 0) {
            fizzBuzzList.add("Fizz");
        }
        else if ((i % 5) == 0){
            fizzBuzzList.add("Buzz");
        }
        else{
            fizzBuzzList.add(Integer.toString(i));
        }
    }
}

public void printFizzBuzz(){
    for (String s : fizzBuzzList){
        System.out.println(s);
    }
}

I personally did not use Java 8 streams as a preference, and I feel like it complicates a simple problem and makes it more than it is.

You could use this in your main or any static method like so:

FizzBuzz fb = new FizzBuzz(1, 101);
fb.printFizzBuzz();

Note that 101 is exclusive and the 1 is inclusive so this will iterate from 1 to 100.

Nexevis
  • 4,647
  • 3
  • 13
  • 22
0

And here is a streams version. It generates a closed range from 1 to 100 and then maps the integers accordingly to the appropriate values using nested ternary operators.

      List<String> list = IntStream.rangeClosed(1, 100).mapToObj(
            n -> n % 15 == 0 ? "FizzBuzz"
                  : n % 3 == 0 ? "Fizz"
                        : n % 5 == 0 ? "Buzz"
                              : Integer.toString(n)).collect(
                                    Collectors.toList());
      System.out.println(list);
WJS
  • 36,363
  • 4
  • 24
  • 39