This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Nested Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested Loop" Watch "Nested Loop" New topic
Author

Nested Loop

Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Ok guys...I have another question for you. I need to write a nested loop. I have to ask a user to input a whole number, and let's say they enter the number 20. I need to output the number in five columns... 1 2 3 4 5
6 7 8 9 10
and so on. I need some sort of loop that counts, and some kind of tab things...any suggestions?


) ) ) )
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I suggest you start with


Seriously, you will need to use a for loop, more than likely. Do you understand the syntax given in your textbook (assuming you have a textbook, of course)? What have you done so far?

If you aren't sure about where to start, perhaps writing some ideas on paper can get the ideas churning. I often find this helpful when tackling a new problem. Alternatively, you can post your thoughts here, including any bits of code you have tried.

Once you show the effort you have made in writing this program, there are many people here that are more than willing to help out. Please let us know what you come up with as a starting point.

Good luck!

Layne
[ October 17, 2004: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195

Layne,

As you can see, I started with..public static void main(String[] args)
Just kidding.
So far, I had the user inputting his/her number, and my program is outputting 1 2 3 4 5 6 and so on, but it's on a straight line, I need my numbers to output in columns ... like..
1 2 3 4 5
6 7 8 9 10 and so on up to 20.

Any suggestions? Yes, I have a book, but boy it's a strange book, not too many examples to go by.


Thanks for your reply. It is greatly appreciated.

(Marilyn added code tags)
[ October 18, 2004: Message edited by: Marilyn de Queiroz ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I'm infernig from your sample output that the problem is to output the integers from one to the user's number, five per line. If so, then you're only missing the last part: "five per line."

Hint 1: "System.out.println();" will cause a carriage return so output starts on the next line.

Hint 2: Look up "modulo" or "mod" in your book.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Ok...I'll see what I can do now. Thanks so much for your help!!!
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Rose Evans
Did you learn MOD and using if condition?
If you have already learnt these, then you can think about MOD and count using if condition to do it.
There are several methods that can be used in your problem, one of the method is given above.
A big hint


Francis Siu
SCJP, MCDBA
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
HI siu chung man,

I looked in my java book and I didn't see anything on MOD...is that modulus? I have just got to work, so after work, I'll search around and see if I can find something.
There's some kind of tabs you can set in your code, but I don't know how to do it.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
I understand that the System.out.println(); will cause a new line, but how to you get it to just stop at the number 5, and then start a new line at 6 to go through 10?
1 2 3 4 5
6 7 8 9 10 and so on?

I am just clueless...
Ramaswamy Srinivasan
Ranch Hand

Joined: Aug 31, 2004
Posts: 295
Hi Rose...

Try thinking in this direction...your requirement is to get a new line, after number 5 and at intervals of 5.

So, the conditions that we look for to get to the next line should be

1. The number is greater than or equal to 5 - (i>=5)
2. The number is to be a multiple of 5 - (i%5==0)

In case the condition turns true, we go for a println()

else we go for print()

This is the idea to go about.

If you still have problems, just mail me for the code...Hope this helps you out.

Cheers,
Swamy
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
Sorry for my unclear statement
Hints:
1: if condition
2: using % (MOD) with your program variable count
If you did not learn % (MOD)
I can talk about what it is
For example
3%2=1, the function of % (MOD) is used to find out the reminder
So 4%2=0, 5%2=1
In your case, if count%??==?? then print out a new line.
:roll:
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Swamy,

I did it!!!Thank you, thank you, thank you!!! Two heads are always better than one!! Tell me, do you program for a living? Or are you a student, or what?

THANK YOU SO MUCH FOR YOUR HELP!!!
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Oh duh on my part, I see student listed under your name. You seem to be really smart at this java stuff!! How long have you been using java?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested Loop