My Origional Code:
The above code is working fine but want to run with nested for loop. I am confused as I have to work with 3 different values e.g.
Test-1 java.lang.ArrayIndexOutOfBoundsException: 1
I am not understanding how can I get them as request.getParameterValues(""); as it throws error after getting first parameter
Thanks in advance and best regards
Let's trace through the new code.
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.
Jeanne Boyarsky wrote:Farakh,
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?
Still waiting for reply of PART-I of my question
Thanks for helping and profound regards
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.
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.