File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Nested Loop Counter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested Loop Counter" Watch "Nested Loop Counter" New topic

Nested Loop Counter

Sarah Creech

Joined: Oct 08, 2004
Posts: 6
I really need some help with this programming assignment. I am suppose to ask a user for a number and from there you are suppose to output the counter in rows of 5 numbers per row.

1 2 3 4 5
6 7 8 9 10
11 12 13 14 and so on I am stuck on the coding part of this this is what I have so far.

// This program is a nested loop that takes in a whole number and counts to what ever number you
// would like it to and puts the numbers into columns.

// Sarah Creech
// October 18, 2004

public class NestedLoop
public static void main(String[] args)

int ctr = 1;
int sum;

System.out.println("Please enter any whole number that you would like me to count to.");

sum = SavitchIn.readLineInt();

System.out.print(ctr + " ");
} while(ctr <= sum);

Anyones help will be appreciated.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

well, from the subject of this post, you probably know you need some kind of loop inside another loop.

If i were you, i'd not try and do it all at once. You've got the input working, i assume. that's a good start.

now look at what you need to output... see if you can analyze it for any patterns...

what i see is that the first number of every line increments by 5 each time. Can you write a single loop that only prints out the

etc. up to your input number? try and get that to work first. once you have that working, we can work on the next part...

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

Joined: Jul 30, 2003
Posts: 805
Hi, Sarah -

I'd like to recommend that you start your design with pseudocode, not with coding. If you can make your design work with pseudocode, you'll be able to "port" it to Java. For example, a nested loop could look like this:

Using pencil and paper, you can see that your output will be:

11 12 13 14 15
21 22 23 24 25 etc.

Now, once you have the pseudocode working, build your Java code from it.

See how that follows the pseudocode? You could even use your pseudocode as comments for the Java, but I'll leave that up to you to work out.

Let me know if that helps, or if you need more.

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Sarah Creech
You can take a look with a similar topic that we have discussed before, I predict that you may know each other.
You can click here
Of course, some rachers gave you some tips and you just require to edit a little bit code that will become an answer.
My suggestion will be that keep the program as simple as possible.
Do you think that a loop is enough?

[ October 18, 2004: Message edited by: siu chung man ]

Francis Siu
Sarah Creech

Joined: Oct 08, 2004
Posts: 6
Thanks Guys for all of the help I finally got it working.
I did not know that anyone had replied because it usually emails me and notifies you when someone does answer your question. Dont know why it did not this time. Thanks everyone for your help.
I agree. Here's the link:
subject: Nested Loop Counter
It's not a secret anymore!