aspose file tools*
The moose likes Java in General and the fly likes Java + Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java + "for" loop " Watch "Java + "for" loop " New topic
Author

Java + "for" loop

Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Hi all,

I am trying to write a servlet which can generate pdf worksheets (using itext) upon receiving users input on my webpage. However, I face difficulty on getting the right layout I need for my pdf worksheet.

My code:

What I am trying to achieve:
Page 1 of the pdf file:
Question 1
Question 2
(up to ....)
Question qno

Page 2 of the pdf file:
Solution to Qn 1
Solution to Qn 2
(up to ....)
Solution to Qn qno

I am aware that it is the 'for' loop that arranges the output in the form of Question 1, followed by Answer 1, then Question 2, followed by Answer 2, and so on.

However, I cannot separate the 'Question' logic and the 'Answer' logic or loop them using 2 different 'for' loop since they are both dependent on the RANDOM variables a1,b1,c1,a2,b2,c2. (I cannot declare the variables outside the 'for' loop as they are supposed to change randomly for each instance of i.)

I have tried solving this problem by working on what itext can offer, but to no avail.

I will be very grateful if anyone could help me on this issue

An advance thanks to all.
Tarun Bolla
Ranch Hand

Joined: Jun 20, 2011
Posts: 89
Instead of writing directly to doc in for loop...write to QuestionsList and AnswersList (See ArrayLists) and then in two new for loops write QuestionList and AnswerList to doc respectively.
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Tarun Bolla wrote:Instead of writing directly to doc in for loop...write to QuestionsList and AnswersList (See ArrayLists) and then in two new for loops write QuestionList and AnswerList to doc respectively.


Hi Tarun Bolla,

Thank you for your prompt reply.

I have changed my program to what you have suggested (I suppose I did the right thing?). However, the doc doesn't seem to accept my QuestionList and AnswerList as valid input String.

My modified code:



My eclipse shows error in the statement "doc.add(new Paragraph(QuestionList));" & "doc.add(new Paragraph(AnswerList));".

2 quick fixes suggested by Eclipse are:
1. Remove argument to match 'Paragraph()' > (It removes the phrase 'QuestionList' & 'AnswerList')
2. Change type of AnswerList to float > (But what I need is String)

Could you kindly advise me on where my mistakes are?

Thank you very much once again.



Tarun Bolla
Ranch Hand

Joined: Jun 20, 2011
Posts: 89
Apple Khoa wrote:for (int i=1; i < qno+1; i++)
{
doc.add(new Paragraph("Question " + i, new Font (Font.HELVETICA, 12, Font.BOLD | Font.UNDERLINE)));
doc.add(new Paragraph("Solve the following simultaneous equations:", new Font (Font.HELVETICA, 12)));
doc.add(new Paragraph(QuestionList));
}

for (int j=1; j < qno+1; j++)
{
doc.newPage();
doc.add(new Paragraph("Solution to Question " + j, new Font (Font.HELVETICA, 12, Font.BOLD | Font.UNDERLINE)));
doc.add(new Paragraph(AnswerList));
}


Those loops need to be changed to



EDIT : CODE EDITED FOR USE WITH LOOP VARIABLES
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Tarun Bolla wrote:
Those loops need to be changed to


EDIT : CODE EDITED FOR USE WITH LOOP VARIABLES


Hi Tarun Bolla,

I have modified my code according to what you have taught, but may I know where should I insert the loop "for (int k=0; k<qno+1;k++)" so that the program knows how many questions (qno is the number of questions requested by users) to generate?

My code (shown below) manages to separate the Questions & Answers, but produces output like:
(If qno is 3)
Question 1
Question 2
Solution to Question 1

Question 1
Question 2
Solution to Question 1

Question 1
Question 2
Solution to Question 1





Can you kindly advise me on this issue?

Your help is greatly appreciated
Tarun Bolla
Ranch Hand

Joined: Jun 20, 2011
Posts: 89
Apple Khoa wrote:



So you say that you are getting the PDF output thrice....
Just remove the outer loop....
for (int k=1; k<qno+1; k++)
Every thing should be fine....

And by the way...doesnt ques 2 have an answer???>

EDIT :
Apple Khoa wrote: I have modified my code according to what you have taught, but may I know where should I insert the loop "for (int k=0; k<qno+1;k++)" so that the program knows how many questions (qno is the number of questions requested by users) to generate?

And you need not use that qno + 1 beacuse you already told the loops how many ques and how many answers are there in the list ... using QuestionList.size() and AnswerList.size() >
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Tarun Bolla wrote:
So you say that you are getting the PDF output thrice....
Just remove the outer loop....
for (int k=1; k<qno+1; k++)
Every thing should be fine....

And by the way...doesnt ques 2 have an answer???>

EDIT :
Apple Khoa wrote: I have modified my code according to what you have taught, but may I know where should I insert the loop "for (int k=0; k<qno+1;k++)" so that the program knows how many questions (qno is the number of questions requested by users) to generate?

And you need not use that qno + 1 beacuse you already told the loops how many ques and how many answers are there in the list ... using QuestionList.size() and AnswerList.size() >




Yes, I am getting the output thrice (as I requested for 3 questions on my webpage) in a single pdf file.

You advised me to remove the outer loop "for (int k=1; k<qno+1; k++)". If that is the case, how to tell the program to generate 3 (or any number requested by user) different set of questions (with corresponding answers) in the format desired?

By the way, yes, only answer to question 1 is displayed.
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Tarun Bolla wrote:
So you say that you are getting the PDF output thrice....
Just remove the outer loop....
for (int k=1; k<qno+1; k++)
Every thing should be fine....

And by the way...doesnt ques 2 have an answer???>


Hi Tarun Bolla,

If I remove the outer loop "for (int k=1; k<qno+1; k++)", and only insert what you have suggested earlier, the output is:

Question 1
Question 2
Solution to Qn 1

(no matter how many set of questions I requested on my webpage)


Could you kindly advise me on this?


Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Hi Tarun Bolla,

Thank you so much for your help! I have spent days trying to solve this problem. Finally, it has been solved!
Tarun Bolla
Ranch Hand

Joined: Jun 20, 2011
Posts: 89
You are welcome Apple Khoa...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java + "for" loop