I have a logic problem. User is allowed to add any number of fields to the form. Upon form submission, I need to collect the values of all the fields and store them in an ArrayList. The problem is that I don’t know whether there will be only 1 field in the form, or 500.
The way I differentiate individual form fields is by adding index number to their name, so in a form there are fields with name=”field1″, name=”field2″ etc.
Here is the code that I have so far:
for (int i = 1; i < number of fields; i++) {
while (request.getParameter("field" + Integer.toString(i)) != null) {
Add to ArrayList
}
}
How can I find the number of fields? Or maybe there is another solution to this problem?
Use a while loop: