This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
1) i and j are 0
2) question array contains 1 value - the value of "question1"
3) print out question - this happens to work since the length of the array is one
4) 1 and j are 1
5) question array contains 1 value - the value of "question2"
6) print out question - problem - the array still only has one element
Note that you don't need both i and j as they are always set to the same value. Consider the following simplified code:
The difference is getParameter() is used to get a single value for a given parameter name. getParameterValues() is used to get multiple values for a given parameter name. Since you don't reuse parameter names, you don't need the extra array.
Farakh khan wrote:I think we can not store more than one value to single string. is that right? In above example can String question get more than one value?
You are correct a String can hold only one value at a time. Your name/value pairs have distinct names though:
Each call to req.getParameter() would return only a single value. It just happens you are calling it more than once with different name values. This is fine. You are still only dealing with one value at a time.
Farakh khan wrote:Still waiting for reply of PART-I of my question
It was a really long post - I didn't notice it had two parts. Reading Part 1 now.
author & internet detective
It looks like Part 1 is a question of how to generate HTML of this form without hardcoding it.
The answer depends on what data structure you are using to store the questions and answers. Since you didn't say, I'll assume it doesn't exist and you get to choose.
To me, a logical data structure would be ArrayList<Question> or Question.
Then Question would contain:
ArrayList<String> or String possibleAnswers - (this would be another object if there is more information than just the string)
With actual data structures, it makes sense to talk about loops. In my example, you need two loops since there are two arrays. The correct answer is the same across all possible answers so it stays at the Question level. Try writing the loop and posting it here for comments/help.