• Post Reply Bookmark Topic Watch Topic
  • New Topic

replace a value of a for loop with a different value  RSS feed

 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rick, please UseCodeTags (← that's a link) when posting code.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junita Eka Sari
Greenhorn
Posts: 8
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
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
And what do you do if the number is neither a multiple of 3 or 5?
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
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
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
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Clapham
Sheriff
Posts: 22520
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've marked the thread as "resolved".
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!