aspose file tools*
The moose likes Servlets and the fly likes Nested Loop and getting parameter problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Nested Loop and getting parameter problem" Watch "Nested Loop and getting parameter problem" New topic
Author

Nested Loop and getting parameter problem

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
PART-I:
====================
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.


PART-II:
================

OUTPUT:
-------
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

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Farakh,

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[0] - 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[1] - 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.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
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
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

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:
question1
question2
etc
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.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

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:
int number
String text
String correctAnswer
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.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Thanks a lot. the problem solved

Appreciated
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Nested Loop and getting parameter problem