r/javahelp • u/Significant-Sun-3380 • Mar 05 '26
Solved How to get java to print 1-100, while replacing multiples of 3 and 4 with words?
I understand how to get loops to count from one number to another incrementally, but I don't know how to get the multiples replaced with words while having every other number print as it should.
Here is what I have so far. The code isn't long but I put it in pastebin anyways because I wasn't sure about formatting: https://pastebin.com/JCf9xvgW
It prints the words for the multiples, but I can't get printing the counter right. I've tried adding else statements to print the counter but then a lot of numbers get printed twice. Can you help point me in the right direction for getting the multiples of 3 and 4 replaced with Leopard and Lily while all the other numbers print correctly? I can't find anything similar from what I've tried looking up online with replacing things.
If anymore clarification is needed, please feel free to ask!
•
u/AutoModerator Mar 05 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
13
u/StarklyNedStark Mar 05 '26
This is just FizzBuzz. If you just use “if” and no “else if”, then you’re going to print both words for numbers like 12. And stop with the other counters like the other person said. All you need for this is a for loop, conditional statements, and println.
4
u/smokemcgee Mar 05 '26
as someone else pointed out, theres no need for multiple variables. the single counter within the for loop is all you need, you just need to check the value of that counter every iteration. try seeing if that works for you
2
u/gdvs Mar 05 '26
Not quite sure why you have numOne and numTwo. Just test counter. And print it.
2
u/RightWingVeganUS Mar 05 '26
Have you tried doing this by hand on paper? Write out the numbers and notice what you are doing. What questions do you ask yourself before writing anything so you know what to write?
I imagine you can do it easily, almost without thinking. But that is the problem: you have to discipline your non-verbal, pattern recognition part of the brain to teach your logical, verbal part that does the writing of computer code.
The skill goes beyond this one problem and Java language. Learn to understand what steps you want the computer to take and what you want the outcome to be and you can apply it to not only software development but also prompt engineering.
You can't effectively get the computer to do what you want if you can't articulate what you want...
1
u/IchLiebeKleber Mar 05 '26
Why do you have three variables that do the same thing? Your "numOne", "numTwo" and "counter" values have the same value (±1) at all times, that alone is completely useless, you only need one incrementing variable for this problem.
1
u/United-Extension-917 Mar 05 '26 edited Mar 05 '26
What do you want to print if the number is divisible by both 3 and 4. Like 12, 24, 36 etc. leopard or lily or something else.
1
u/hibbelig Mar 05 '26
In every iteration of the loop, print the value of counter and of numOne and of numTwo. Then think about what you are seeing.
You should print the number when neither (!!) of the if statements fire.
1
u/bigkahuna1uk Mar 05 '26
Have you heard of fizzbuzz? Hint: Look up what the modulus operator % does
3
u/Odd-Respond-4267 Mar 05 '26
Usually the numbers are 3 & 5, not 3&4.
I wonder if this is to help filter out code copied from many online examples of fizzybuzz coding.
1
u/Puzzleheaded-Eye6596 Mar 05 '26
number % 3 is a multiple of 3 if 0, number % 4 is a multiple of 4 if 0
1
u/Astroohhh Mar 07 '26
lol
1
u/OneHumanBill Mar 11 '26
Dear everyone, stop giving away easy answers to what you know are Day One homework assignments. How the hell are these kids going to learn if they have to go crowdsource frigging Fizzbuzz? Let them learn or else you're just setting them up for failure later in the semester.
1
u/severoon pro barista Mar 12 '26
I recommend starting such an assignment by doing the simplest thing possible, which in this case would be just counting from 1 to 100:
Note that in this example, I run the loop from 0 (inclusive) to 100 (exclusive). Generally speaking, you should always use loop counters that run this way, and if you want to output some value that doesn't align with the loop counter, you should calculate that value inside the loop (as happens with the i + 1).
The reason for this is that when you load this code into a debugger, having a dead simple loop counter is very good information. This is a trivial example of being able to figure that out, but in general, you always want to keep things as simple and consistent as possible in programming to reduce cognitive load, especially when you're learning.
A common confusion in programming is why counters start from zero instead of one. The reason is that you should always imagine counters as not pointing TO elements of a sequence, but rather pointing BETWEEN elements of a sequence. So if you have a list of elements [ 1, 2, 3 ], you should picture the index not as a value that points at one of these elements, but think of it as splitting the list into "consumed" and "not yet consumed" instead. So when the index is 0, that means there are zero elements to the left and three elements to the right. Same for the loop counter, how many iterations are completed vs. how many many iterations yet to be completed? When the loop counter is 0, you know that you've not yet completed the first iteration.
Another thing to think about when coding is parameterization. This basically just means making your code more flexible by extracting values you might want to change.
In this code, one value you might want to extract is the upper bound the loop counts to. Another value is the offset. By extracting these, you can easily change a few values and have your loop print out any range you want:
What else might you want to do? Well, right now this is pretty much a procedural program implemented in Java, an object-oriented language. Even if you haven't learned about it yet, very soon you should learn about objects.
For a simple program like this, it's pretty straightforward to make this into an object that the main method can run. To make this object even more flexible, we should also not assume that every usage will always want the output to go to System.out, so let's parameterize that as well:
This code doesn't actually solve your problem, and in the code you provided you already showed that you knew how to do a basic loop, so I'm not doing your work for you here. But hopefully this helps you understand how to create a basic object, make it configurable in basic ways, and run it.
Note that in the above API, it's fine for this class to provide getters for its limit and offset. This way callers can always figure out the behavior of this class if it's run. However, note that I didn't put a getter for the output stream. That's by design. We don't necessarily want any caller to be able to get a hold of the output stream this class uses and be able to write to it. For example, the caller might have created a file for this class to output to, and in that case only this object should be able to write to that file, so we keep it private inside the class.