patternjavaModerate
Simple FizzBuzz
Viewed 0 times
simplefizzbuzzstackoverflow
Problem
I wanted to try FizzBuzz, but to do it in the most efficient way. I think this method employs most of the concepts I learned here. Is there anything unaccounted for? A sprinkle of Swing since that's what I've been focusing on lately.
import javax.swing.JOptionPane;
public class FizzBuzzCounter {
public static void main(String[] args) {
int goalNumber = Integer.parseInt(JOptionPane.showInputDialog("FizzBuzz goal number?"));
fizzBuzzify(goalNumber);
}
public static void fizzBuzzify(int n) {
for (int i = 1; i <= n; i++) {
StringBuilder sb = new StringBuilder();
if (i % 3 == 0) sb.append("Fizz");
if (i % 5 == 0) sb.append("Buzz");
if (sb.toString().isEmpty()) System.out.println(i);
else System.out.println(sb.toString());
}
}
}Solution
There are a bunch of style issues you should be aware of, a validation problem, an optimization or two, and also an alternate approach:
Style:
Use braces for 1-liners... these are 'no-no' statements:
and should be:
Validation
Your OptionPanel blindly assumes the input will be a valid integer. This is not true, and you should build a validation sequence for it.
You should also catch 'NumberFormatException' from the
Performance & alternate
The StringBuilder may make sense, but, if you use it, there are two things:
But, in reality, there is no need for the StringBuilder if you use plain String Constants, and also use else-statements, and also use
Style:
Use braces for 1-liners... these are 'no-no' statements:
if (i % 3 == 0) sb.append("Fizz");
if (i % 5 == 0) sb.append("Buzz");
if (sb.toString().isEmpty()) System.out.println(i);
else System.out.println(sb.toString());and should be:
if (i % 3 == 0) {
sb.append("Fizz");
}
if (i % 5 == 0) {
sb.append("Buzz");
}
if (sb.toString().isEmpty()) {
System.out.println(i);
} else {
System.out.println(sb.toString());
}Validation
Your OptionPanel blindly assumes the input will be a valid integer. This is not true, and you should build a validation sequence for it.
You should also catch 'NumberFormatException' from the
Integer.parseInt(...)Performance & alternate
The StringBuilder may make sense, but, if you use it, there are two things:
- use
sb.length() == 0instead ofsb.toString().isEmpty()which creates an unnecessary String value.
- create the sb instance outside the loop, and reuse it, and
sb.setLength(0)inside the loop to clear it.
But, in reality, there is no need for the StringBuilder if you use plain String Constants, and also use else-statements, and also use
%15 (which is %3 and %5).if (i % 15 == 0) {
System.out.println("FizzBuzz");
} else if (i % 3 == 0) {
System.out.println("Fizz");
} else if (i % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(i);
}Code Snippets
if (i % 3 == 0) sb.append("Fizz");
if (i % 5 == 0) sb.append("Buzz");
if (sb.toString().isEmpty()) System.out.println(i);
else System.out.println(sb.toString());if (i % 3 == 0) {
sb.append("Fizz");
}
if (i % 5 == 0) {
sb.append("Buzz");
}
if (sb.toString().isEmpty()) {
System.out.println(i);
} else {
System.out.println(sb.toString());
}if (i % 15 == 0) {
System.out.println("FizzBuzz");
} else if (i % 3 == 0) {
System.out.println("Fizz");
} else if (i % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(i);
}Context
StackExchange Code Review Q#61994, answer score: 12
Revisions (0)
No revisions yet.