aspose file tools*
The moose likes Beginning Java and the fly likes program running successfully just need a minor modification  to make it 100% correct Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "program running successfully just need a minor modification  to make it 100% correct" Watch "program running successfully just need a minor modification  to make it 100% correct" New topic
Author

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

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

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


http://www.lifesbizzare.blogspot.com || OCJP:81%
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

Please use code tags when you post source code, that will make it much easier to read on the forum.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
krishna bala
Ranch Hand

Joined: Jul 20, 2009
Posts: 62
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

Joined: Jul 31, 2009
Posts: 37
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

Joined: Jul 31, 2009
Posts: 37
Also consider using the Modulus operator to do your comparison: Modulus. It should make your code a lot simpler.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
krishna bala
Ranch Hand

Joined: Jul 20, 2009
Posts: 62
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

Joined: Oct 02, 2003
Posts: 11499
    
  16

Krishna,

Please check your Private Messages for a note from me.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
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.


Joanne
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

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

Joined: Aug 01, 2009
Posts: 1056

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

Joined: Jul 15, 2009
Posts: 537

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


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

okkk, is it necessary to use array over here
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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

Joined: Aug 01, 2009
Posts: 1056

ok you mean bar ,bazz,foo
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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

Joined: Aug 01, 2009
Posts: 1056

ok you mean call this multiples in constructors and assign it to variables ???
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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

Joined: Jul 20, 2009
Posts: 62
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

Joined: Jul 31, 2009
Posts: 37
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: program running successfully just need a minor modification to make it 100% correct