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.