HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Fizz Buzz Bizz Fuzz in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
buzzbizzjavafuzzfizz

Problem

This questions is originally from http://contestcoding.wordpress.com/2013/06/28/fizz-buzz-bizz-fuzz/.



  • Print the integers from 1 to 100,



  • but for the multiples of 3, print "Fizz" instead and



  • for multiples of 5, print "Buzz".



  • If the number contains a 3 (for example 23), print "Bizz" and



  • if the number contains a 5, print “Fuzz”



  • (if it contains multiple 3s or 5s, just print one "Bizz" or "Fuzz").



  • If the number contains more than one of these attributes, print every word (for example 33 prints "FizzBizz", as 33 is both a multiple


of 3 and contains the digit 3).


My Java solution is:

public class FizzBuzz {
    public static void main(String[] args) {
        for (int i = 1; i < 101; i++) {
            // Set this to true when one of the special conditions is met.
            boolean printed = false; 

            if (i % 3 == 0) {
                // When i is divisible by 3, then print "Fizz"
                printed = true;
                System.out.print("Fizz");
            } else if (i % 5 == 0) {
                // When i is not divisible by 3 but is divisible by 5, then print "Buzz"
                printed = true;
                System.out.print("Buzz");
            }

            if (Integer.valueOf(i).toString().indexOf("3") != -1) {
                // When i has the digit 3 in it, then print "Bizz"
                printed = true;
                System.out.print("Bizz");
            } else if (Integer.valueOf(i).toString().indexOf("5") != -1) {
                // When i has the digit 5 in it, then print "Fuzz"
                printed = true;
                System.out.print("Fuzz");
            }

            if (printed == false) {
                // The number does not satisfy any of the special conditions above.
                System.out.print(i);
            }
            System.out.println();
        }
    }
}


Please provide code review comments.

Solution

Overall, this is a pretty straightforward program. See the bottom of my answer for an Extreme Makeover: Code Edition of the program.

You have a hard-coded "magic number" in your for loop. It would be better to use a variable.

for (int i = 1; i < 101; i++) // not the best

int num = 101;
for (int i = 1; i < num; i++); // better


Your if test conditions can be shortened a bit.

if (Integer.toString(i).indexOf("3") != -1)


Your logic is a bit off. You should actually have fewer else if conditions (this is rarely the case, but here it is applicable). For example, when i reaches "15", is should print "FizzBuzzFuzz", but your program only prints "FizzFuzz".

Final code:

public class Test
{
    public static void main(String... args)
    {
        int num = 101;
        for (int i = 1; i < num; i++)
        {
            boolean printed = false;

            if (i % 3 == 0)
            {
                printed = true;
                System.out.print("Fizz");
            }
            if (i % 5 == 0)
            {
                printed = true;
                System.out.print("Buzz");
            }

            if (Integer.toString(i).indexOf("3") != -1)
            {
                printed = true;
                System.out.print("Bizz");
            }
            if (Integer.toString(i).indexOf("5") != -1)
            {
                printed = true;
                System.out.print("Fuzz");
            }

            if (printed == false) System.out.print(i);
            System.out.println();
        }
    }
}


Extreme Makeover: Code Edition

Let's use a StringBuilder and some ternary operators. And let's get rid of that boolean.

public class Test
{
    public static void main(String... args)
    {
        int num = 101;
        for (int i = 1; i < num; i++)
        {
            StringBuilder sb = new StringBuilder();
            if(i % 3 == 0) sb.append("Fizz");
            if(i % 5 == 0) sb.append("Buzz");

            if(Integer.toString(i).indexOf("3") != -1) sb.append("Bizz");
            if(Integer.toString(i).indexOf("5") != -1) sb.append("Fuzz");

            if (sb.length() == 0) System.out.print(i);
            else System.out.print(sb);
            System.out.println();
        }
    }
}

Code Snippets

for (int i = 1; i < 101; i++) // not the best

int num = 101;
for (int i = 1; i < num; i++); // better
if (Integer.toString(i).indexOf("3") != -1)
public class Test
{
    public static void main(String... args)
    {
        int num = 101;
        for (int i = 1; i < num; i++)
        {
            boolean printed = false;

            if (i % 3 == 0)
            {
                printed = true;
                System.out.print("Fizz");
            }
            if (i % 5 == 0)
            {
                printed = true;
                System.out.print("Buzz");
            }

            if (Integer.toString(i).indexOf("3") != -1)
            {
                printed = true;
                System.out.print("Bizz");
            }
            if (Integer.toString(i).indexOf("5") != -1)
            {
                printed = true;
                System.out.print("Fuzz");
            }

            if (printed == false) System.out.print(i);
            System.out.println();
        }
    }
}
public class Test
{
    public static void main(String... args)
    {
        int num = 101;
        for (int i = 1; i < num; i++)
        {
            StringBuilder sb = new StringBuilder();
            if(i % 3 == 0) sb.append("Fizz");
            if(i % 5 == 0) sb.append("Buzz");

            if(Integer.toString(i).indexOf("3") != -1) sb.append("Bizz");
            if(Integer.toString(i).indexOf("5") != -1) sb.append("Fuzz");

            if (sb.length() == 0) System.out.print(i);
            else System.out.print(sb);
            System.out.println();
        }
    }
}

Context

StackExchange Code Review Q#38178, answer score: 7

Revisions (0)

No revisions yet.