I've been taking the Coursera compilers class recently. In the spirit of that...here's a solution using the Lexical Analysis tool JFlex.
The following input file will generate a finite-automata based lexical analyser that will parse an input stream of space/new line delimited numbers and produce the required output. So you just need to feed it a stream containing the range of numbers you want to use.
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
Your program doesn't do this. Instead of "Fizz", "Buzz" or "FizzBuzz" it prints "THREE", "FIVE" or "THREE AND FIVE". Also, in your program the range goes from -50 to 50, not from 1 to 100. So it's not a correct solution for the Fizz Buzz problem.
Here's what I think is an interesting take on the challenge:
- recusive solution
- certainly not idiomatic Java
- not at all efficient (might blow the stack if requirements were to change to print 100K elements)
- but it is functional
- and it is pure Java
The model is that we have a Producer, which produces a stream of integers, a Transformer, which converts to fizz/buzz as appropriate, and a Consumer, which prints them out. These are chained together, but each runs in its own thread. Consumers will happily wait for input from producers as necessary.