A friendly place for programming greenhorns!
Chapter Loops
I need help..I have an assignement on chapter loops which I dont understand..can someone help..
it starts
enter number or parts:
enter number of chapters:
beginning part 1
chapter 1
chapter2
chapter3
chapter 4
End part 1
Begin part 2
chapter 1
chapter 2
chapter 3
chapter 4
end part 2
begin part 3
chapter 1
chapter 2
chapter 3
chapter 4
end part 3

how do I get java to do this ??

how do I get java to do this ??

From your description it appears that you need to print a list of parts and a list of chapters.

To start, forget about the chapters and focus on the parts.

So how can you print a list of parts? You could hard code a bunch of print statements. But a better way is to use a loop. The loop starts at 1 (the first part) and ends at the highest part number (say 3 as in your example).

Now stop and write the code to print just the parts.

I'll wait.......... Done? Ok, good.

Now think about the chapters for a given part. It's the same problem. You need to loop from 1 to the number of chapters.

But wait the output is interleaved, you need to print part 1 and then it's chapters, part 2 and it's chapters, etc. How do you do that? Well the answer is that you need to nest the two loops -- put one inside the other.

You start the outer (part) loop, on the first time through the loop (part no = 1), you need to print all of the chapters -- loop from chapter 1 through chapter 4. Then repeat this chapter loop each time through the part loop.

I hope that's enough to get you started. Try to code the solution starting with the outer "parts" loop and get it working correctly. Then start on the inner "chapter" loop. If you get stuck, post your code and ask a specific question.
can you post this in code? please show me in code.

Originally posted by doraine lee:
can you post this in code? please show me in code.

You have been helped many, many, many times.
typicalebayer,

Run this code, It should give you the output you need.

[ October 12, 2006: Message edited by: fred rosenberger ]
Hi,

You can do this very easily, you have take a for loop to dispaly chapters and one more for loop to display the parts,ok i will give you the way
suppose you have passed the values for parts as 4 and chapters as 4 as command line arguments

[ October 12, 2006: Message edited by: fred rosenberger ]

can you post this in code? please show me in code.

No. You need to show us your code, explain what your problem is and we'll guide you.

I wouldn't be helping you by giving you the answer.
that's ok
i never send code again