• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

program running successfully just need a minor modification to make it 100% correct

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys our class professer gave us a question that was to create a program that loops from 1 to 50 and prints each value on a seperate line .also print foo for every multiple three line,bar for every multiple of five and baz for every multiple of seven.So the program i wrote was like this



the console output was like this:
1
2
3 foo
4
5 bar
6 foo
7 baz
8
9 foo
10 bar
11
12 foo
13
14
15foo...i want to display it as 15 foo bar
15bar..i want to display it as 15 foo bar
.
.
.
50 bar

so please let me knw wats da modifications need 2 be made
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15458
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags when you post source code, that will make it much easier to read on the forum.
 
krishna bala
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young: Solution deleted - please let Vishal do his own homework. We're here to help people learn Java - you can give him hints, but don't post a solution without any further explanation.
 
Jarred Olson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would assume that you want "21 foo baz" in the output too since 21 is a multiple of 3 and 7. Instead of having 3 places that you output the text create a text variable at the beginning of each loop in your for loop, then add text to it for each case that is met and output the text at the end of the loop. If you'd like it to perform better (since Strings are immutable and traditional String concatenation wastes memory) use a StringBuilder. Let me know if you need more guidance.
 
Jarred Olson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also consider using the Modulus operator to do your comparison: Modulus. It should make your code a lot simpler.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow is that complicated logic...

this confuses me:


I mean, if the outer condition is true, won't the inner one also always be true?

I would suggest you look at using the modulus operator '%'.

when used with integers, it returns the remainder. so 28 % 5 would return 3. 28 % 7 would return 0.

Can you see how to use this?
 
krishna bala
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr Jesper Young , you should not delete my answer , if you have any thiing to clarify with me , please ask . do not do once again
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krishna,

Please check your Private Messages for a note from me.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jarred Olson wrote:IInstead of having 3 places that you output the text create a text variable at the beginning of each loop in your for loop, then add text to it for each case that is met and output the text at the end of the loop.


That was my first thought as well. Then I thought - why not just change all the System.out.println()s to System.out.print(). Print the value of i at the start of the loop instead of in each S.o.p and print a new line at the end of the loop.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welll fred i dont know whether it is complicated logic but i am developing my logic and the logic i have applied is giving me correct output but with a slight difference(almost 99% correct) ...yeah i heard about modulus it is used for deriving remainders so how can that be of such help in this case my brain not working in terms of modulus
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok fred you mean that the modulus sign is very much same as the equal sign when its output is Zero
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are printing i on every if statement and also doing a println. That's the cause. Print the number to be checked once and let the printing job be done in the check statements. Also using modulo will be much better and storing the multiples in an array and looping it. Well think about it.... Also make the ifs more compact using the short circuit operator &&. It would look much better

Cheers
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okkk, is it necessary to use array over here
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naah not necessary.... I told array coz you can take the multiples from the command line and then use the arg array and then you can print this same program for any multiple that you enter.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok you mean bar ,bazz,foo
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i meant multiples as in 3 5 7 11 12. Well i guess your program is fixed for the homework so you don't need to worry about this.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok you mean call this multiples in constructors and assign it to variables ???
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never said constructors. I hope you have tried taking in parameters from the command line and displaying it in the java program. These command parameters go into String[] args from the main method. So if you pass any number like 1 2 3 in the command line, we can access it as arg[0] and so on. Also in your program the starting if statement which has a m, Why do you want to do that?
 
krishna bala
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi just use the '%' (modula) operator .


in your method first print 'i ' as it is
then check i%3 == 0
print FOO

then check i%5 == 0 print bar
then check i%7== 0
print baz

then print space .
 
Jarred Olson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Modulus is easy to use and helpful once you grasp the concept. Think of it as division (easy to do since the symbols are similar / vs %). Only Modulus returns the remainder as an int. Examples:


This will help you to determine when you have a multiple of another number. If x % i = 0, then i is a multiple of x. Hope this helps with Modulus... and your homework
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic