programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# replace a value of a for loop with a different value

Ranch Hand
Posts: 90
i have a for loop that prints from 1 to 25 .
i need to replace every multiple of 3 and 5 with a word. my problem is that i cant replace the multiples with actual "word" it prints out all the numbers with word on top of each multiple, instead of replacing the multiple with word.
i tried moving the location of System.out.println(i); around but no luck.
how can i interrupt the count and introduce my value?

Sheriff
Posts: 11629
187
Hi Rick, please UseCodeTags (← that's a link) when posting code.

Junilu Lacar
Sheriff
Posts: 11629
187
Trial and error is not a good strategy for writing a correct program. You need to understanding what you're actually telling the computer to do. When you tell it to print out "word", there are conditions that need to be met right?  When you tell it to print out the value of i, have you specified a condition for doing that as well?

Bartender
Posts: 585
9
you should use an if-elseif-else construct:
If multiple of 3, then print the three word
else if multiple of 5, then print the five word
else print the integer

But there is a problem: it might be a multiple of bothe 3 and 5. So you need to determine the full logic and code it accordingly.
You should not write any code until you have worked out the algorithm on paper.

Greenhorn
Posts: 8
What Fred Kleinschmidt is true. You have to use an if-elseif construct.
And please use code tags when you want to post your code

Bartender
Posts: 11445
18
Fred is correct. 1st step: Write in simple english (without any code) how you will solve the problem.
Now since you have already attempted the code, lets see what the logic in it is.

Now do you see what is causing this problem?

rick pine wrote:it prints out all the numbers with word on top of each multiple, instead of replacing the multiple with word.

Once you identify and fix this, you will need to address,

Fred Kleinschmidt wrote:But there is a problem: it might be a multiple of bothe 3 and 5. So you need to determine the full logic and code it accordingly.

rick pine
Ranch Hand
Posts: 90
Thinking about it,  i come to this idea.
Define max value
Iterate from 1 through max value
If there is a number that is a multiple of 3
Sub that number with word
If there is a number that is multiple of 5
Sub that number with word
If they are both multiple of 3 and 5
Sub with word

Sheriff
Posts: 4576
128
And what do you do if the number is neither a multiple of 3 or 5?

rick pine
Ranch Hand
Posts: 90
I got it!!! ... i think. This is the way i see it.
So i was saying , if i is a multiple of 3 print word, if i is a multiple of 5, print word.
but i wasnt saying what to do if i was NOT a multiple of either.
This is what i have now,

This runs fine, but please tell me if there is anything that can be impoved. within the scope of my beginner knowledge.

Junilu Lacar
Sheriff
Posts: 11629
187
Rick,

This is a common pattern that cascades if-else-if-...

Line 6 is only executed if all of the preceding conditions are false so there's no need to explicitly check that condition.

Bartender
Posts: 768
27
In if-then-else Statements there is else clause which will get executed when all if conditions evaluate to false means when i is neither multiple of 3 nor 5 then the code you want to execute in this situation will be in else block. Please go through this, click here --->if-then-else Statements

If you want to do something else than just printing word when i is multiple of 3 and 5 then you need to think about It yet.

rick pine
Ranch Hand
Posts: 90
Thanks for your help guys, you are all correct.
It was just a if else chain. It took me a while but i got it working.
Also , is there a way to close the questions once there has been a conclusion?

Sheriff
Posts: 23262
46
I've marked the thread as "resolved".