• Post Reply Bookmark Topic Watch Topic
  • New Topic

FizzBuzz Problem  RSS feed

 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I need some help for FizzBuzz problem. I want to Write a method String fizzBuzz(int start, int end) that returns a String that contains the numbers from start to end listed this way, the consecutive numbers separated by a comma and a single whitespace. For example, the call fizzBuzz(9, 16) would generate the string “fizz, buzz, 11, fizz, 13, 14, fizzbuzz, 16”. For efficiency, this method should internally use a StringBuilder to build up the result string, and convert it to String to be returned in the end.
This is my code:
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you describe the problem your having?
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is your question?
Does the code provided work?
BTW the code given does not answer the question as it uses a String array rather than a StringBuilder - is this your problem?
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW the code given does not answer the question as it uses a String array rather than a StringBuilder - is this your problem?

yes, how can i use StringBulider?
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find that by Googling java stringbuilder example.
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to above criteria for this method, this method has to work correctly for an empty range where start > end, and a singleton range where start == end.)
I feel i should put another condition in if loop, also when i compile this code, it has error "missing return statement". Can you help me?



 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ashkan ahmadi wrote:when i compile this code, it has error "missing return statement". Can you help me? 
Method fizzBuzz() has String return type so you must return String after loop.

Why are you returning String in else clause in loop? did you notice now you have separate if then statements, where in your previous code you had if then else ladder?

When i is 15 then code inside all three if statement gets executed, do you want String to have value "fizzbuzzfizzbuzz" when i is 15?

In addition to above criteria for this method, this method has to work correctly for an empty range where start > end, and a singleton range where start == end.) 

You can write if condition and throw IllegalArgumentException when start > end. What do you want to do when start == end?

 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fizzbuzz is a children’s educational classroom game (and relevant to this course due to its tragicomical Internet-famous programming-related backstory) where you try to quickly say the numbers in given range so that whenever the number is divisible by 3, you don't say that number but say “fizz” instead. If a number is divisible by 5, you say “buzz” instead, and if it is divisible by both 3 and 5, you say “fizzbuzz”. Otherwise you just say that number. Write a method String fizzBuzz(int start, int end) that returns a String that contains the numbers from start to end listed this way, the consecutive numbers separated by a comma and a single whitespace. For example, the call fizzBuzz(9, 16) would generate the string “fizz, buzz, 11, fizz, 13, 14, fizzbuzz, 16”. For efficiency, this method should internally use a StringBuilder to build up the result string, and convert it to String to be returned in the end. This method must work correctly for an empty range where start > end, and a singleton range where start == end.)
these are my codes;


i don't know what to write in else conditions to return the number as problem says. in addition, how can i get the right output when start == end?


 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A major problem with your code is that it is not indented properly.  See this Style Guide.  Many beginning programmers make the mistake that formatting is just making things pretty.  It's much more than that.  It reveals your program's structure.

Here's what your program looks with a formatting style:

There's a compile error that becomes obvious because of a misplaced brace and the if structures have problems.  Can you see them now?
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I tried a lot to run it and get the the problem to be fixed, but i could not. would you please help me.
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Two problems with this logic. You'll need to use if...else if...else if...else. Secondly, if it is divisible by 15 it is therefore divisible by 3 and by 5 so you'll have to move the test for 15 to the first if.
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't know what to write in else conditions to return the number as problem says. I mean, when it is not divisible by 3 or 5 or 3&5, it must return the number in string form.  in addition, how can i get the right output when start == end?
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fort the 'else' part, all you need is

When the loop is done, then

which you already have.

This will however leave a trailing ", " on the 'sb'. If you want you could use StringBuilder methods to remove it.
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know if you figured this out yet

The compilation error is because you are declaring another variable name 'i' that is in conflict with the one declared in the outer loop. And BTW, you only need one of those loops.
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I write the code. Its logic looks acceptable, but i don't know why does it just apply for the first if loop?
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean lines 23 and 43 for IF LOOPS are not working when the inputs are designated values.
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean lines 23 and 43 for IF LOOPS are not working when the inputs are designated values.
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean lines 23 and 43 for IF LOOPS are not working when the inputs are designated values.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by reading lines 60 to 70 inclusive. You have eleven consecutive }s separated only by whitespace, and not correctly indented. The number of nested ifs and loops looks suspiciously high, and there is an error which is obscured by the multiple nesting and incorrect indentation. Start by moving all that code out of the main method. I think you probably don't need most of those ifs and loops.
You cannot tell whether the logic in that method is acceptable or not.
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't stress enough how important it is to maintain clear and consistent code formatting. Here's your code again re-formatted:

 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now you might be able to see that you're missing a few closing braces after line 20, and again after line 37, and too many after line 55.

Also,

would be clearer if written as
This would also fix the aforementioned missing brace issue.

You have
Only one of those could be true yet you have them nested

[edit] Do you really need to handle the case where start > end? Should this be an error? Could you swap start and end? Or would you have to count backwards?
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:. . . Do you really need to handle the case where start > end? Should this be an error? Could you swap start and end? Or would you have to count backwards?
But counting backwards would go in a different method, surely. Part of the problem of that code is what I hinted at yesterday: trying to squeeze too much code into the main method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!