• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested Loop and getting parameter problem

 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. the problem solved

Appreciated
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic