• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fuzzy- finding Divisible numbers  RSS feed

 
Mohammed Rashid
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am new to both Java Programming and this forum. My interest in Java leads me to try and print numbers from 1-100. The output should show all numbers from 1-100 and each number that is divisible by by 13 should be replaced with a string "Fuzzy". Can an expert please tell me what is wrong with my code?


 
Mike Degteariov
Ranch Hand
Posts: 145
8
Java Mac MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Muhammed,

I can see two distinct problems in your code.

First, you code won't compile because of two missing closing braces.

The number of opening and closing braces should match, you know, but here there are two opening and zero closing curly braces in your class.

Another problem is in "if" statement. There, you only print for the numbers that are divisible by 13, but not for others, so for that "if" statement you need "else" secton.

Please find corrected class below. Hope it helps, let me know if you have further questions.

 
Kevin Mills
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code doesn't do what you describe - your if statement tests it properly, but your print statement doesn't do any kind of replacement. It attempts to add i (an int) and "fuzzy" (a String), which you can't do. Think about what an if-statement does - the lines will only be executed if the test is true. So, what do you want to do if your test is correct (that is, i is a multiple of 13)? You want to print the String "fuzzy". So, we have:



Now, we need to add in the other line. What do you want to happen if the test fails - if the number isn't a multiple of 13? You want to print the number. To determine what happens when a test is failed, use the keyword else. This gives us:



Note that, in a construct



Only one statement will be executed. For us, this means that, in a particular iteration of the loop, either fuzzy or the value of i will be printed, never both. Which is exactly what we want.

Technically, the above code fits your specifications. However, before you try to run it, I will warn you. There will be no spaces or linebreaks in the output. You will get, all on one line 123456789101112fuzzy14... etc. For sanity's sake, I'll add in a space between everything. We can do this by adding in an unconditional print statement to print a space.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Mills wrote:It attempts to add i (an int) and "fuzzy" (a String), which you can't do.

Yes you can, although I agree it's not what the specification asked for.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Others: please do not give complete answers like that. The OP will learn far more by working it out for themselves.
I think the brace problem is simply how the code was copied. I have added code tags (always use them: doesn't it look better now ) and the closing }}.
As Joanne says, you are in fact adding i to fuzzy, but look in the Java Language Specification to see exactly what you are doing.
look up Fizz Buzz on Google because what you are doing is writing a sort of FizzBuzz program.
 
Mohammed Rashid
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Others: please do not give complete answers like that. The OP will learn far more by working it out for themselves.
I think the brace problem is simply how the code was copied. I have added code tags (always use them: doesn't it look better now ) and the closing }}.
As Joanne says, you are in fact adding i to fuzzy, but look in the Java Language Specification to see exactly what you are doing.
look up Fizz Buzz on Google because what you are doing is writing a sort of FizzBuzz program.



Ok thank you guys. It works for me now, I will take a look at the link you have given me!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!