-1

So I am trying to solve the FizzBuzz problem in Java by altering a String ArrayList and then returning a string array after converting the aforementioned ArrayList to a String array

Here's what I have so far:

import java.util.ArrayList;
public class FizzBuzz 
{
    public static void main(String args[])
    {
        ArrayList<String> arrList = new ArrayList<String>();
        for (int i = 1; i<=100; i++)
        {
            arrList.add(String.valueOf(i));
        }

        for (int i = 0; i <arrList.size(); i++)
        {
            if (Integer.parseInt(arrList.get(i)) % 3 == 0 && Integer.parseInt(arrList.get(i)) % 5 == 0)
            {
                arrList.set(i, "FizzBuzz");
            }
            if (Integer.parseInt(arrList.get(i)) % 3 == 0)
            {
                arrList.set(i, "Fizz");
            }
            if (Integer.parseInt(arrList.get(i)) % 5 == 0)
            {
                arrList.set(i, "Buzz");
            }   
        }

        String[] arr = arrList.toArray(new String[arrList.size()]);
        System.out.println(arr);
    }
  }

The error it gives me states:

   "Fizz"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at FizzBuzz.main(FizzBuzz.java:23)

I understand that this error has something to do with not being able to check whether the string element in the arrayList is a multiple of 3,5,etc but that is why I make use of the Integer.parseInt() method which is why I am confused as to what I am doing incorrectly.

Any help would be appreciated, thank you.

3 Answers3

1
import java.util.ArrayList;
public class FizzBuzz 
{
    public static void main(String args[])
    {
        ArrayList<String> arrList = new ArrayList<String>();
        for (int i = 1; i<=100; i++)
        {
            arrList.add(String.valueOf(i));
        }

        //problem lies with all the if statements change them to if-else
        for (int i = 0; i <arrList.size(); i++)
        {
            if (Integer.parseInt(arrList.get(i)) % 3 == 0 && Integer.parseInt(arrList.get(i)) % 5 == 0)
            {
                arrList.set(i, "FizzBuzz");
            }
            if (Integer.parseInt(arrList.get(i)) % 3 == 0)
            {
                arrList.set(i, "Fizz");
            }
            if (Integer.parseInt(arrList.get(i)) % 5 == 0)
            {
                arrList.set(i, "Buzz");
            }   
        }
        //this will print the list
        arrList.stream().forEach(x -> System.out.println(x));
    }
  }

It is because after you change 3 to "Fizz", this will throw an error -> Integer.parseInt("Fizz"). I have added a comment, to change if to if-else.

Hope this helps.

Raushan
  • 161
  • 10
0

The problem is that after your number 3 enters the if %3 and if %5 it also enters the separate if's for both %3 and %5 and it will basically check if "FizzBuzz" converted to an integer will be %3.

The solution is to transform the if (%3) and if (%5) into else if (%3) and else if (%5)

  • Also the printing at the end is not correct, I think this way you will print the hashcode of that list. I recommend looping through it and printing it one by one. – Șold Andrei-Alexandru May 17 '20 at 23:23
  • Lists stringify to `[item1,item2,...]`, where `item1` is the `.toString()`-ed form of the first item, and so on. – tevemadar May 18 '20 at 07:47
0

And God created else...

You should make it so that when an if hits, the if(s) following it do not run. Otherwise they would try parsing the freshly replaced string as a number. Practically: write an else in front of the second and third ifs.

tevemadar
  • 12,389
  • 3
  • 21
  • 49