I am making a form with variable which consists of both String and Integer variables.
ie the parameters that I'm getting from the page1.jsp page are name1,place1,country1,name2,place2,country2,name3,place3,country3.
I want to get these variables and print on the page2.jsp.
This is what I have tried so far.
for(i=1;i>=3;i++)
{
String name(i) = request.getParameter("name"+i);
String place(i) = request.getParameter("place"+i);
String country(i) = request.getParameter("country"+i);
}
for(i=1;i>=3;i++)
{
out.println(name(i));
out.println(place(i));
out.println(country(i));
}
the ERRORS I'm getting are:
Syntax error, insert ";" to complete BlockStatements
6: int i;
7: for(i=1;i>=j;i++)
8: {
9: String name(i) = request.getParameter("name"+i);
10: String place(i) = request.getParameter("place"+i);
11: String country(i) = request.getParameter("country"+i);
12: }
An error occurred at line: 9 in the jsp file: /page2.jsp Type mismatch: cannot convert from String to int
An error occurred at line: 10 in the jsp file: /page2.jsp
Syntax error, insert ";" to complete LocalVariableDeclarationStatement
you are not using an array
try to make it this way
String[] name = new String[3];
String[] place = new String[3];
String[] country = new String[3];
for(int i=0;i<3;i++)
{
name[i]= request.getParameter("name"+i);
place[i] = request.getParameter("place"+i);
country[i] = request.getParameter("country"+i);
}
for(int i=0;i<3;i++)
{
out.println(name[i]);
out.println(place[i]);
out.println(country[i]);
}
I dont know about your previous page from which the value is coming..
to store in an array, there is also a another method
String[] name= request.getParameterValues("name");
String[] place= request.getParameterValues("place");
String[] country= request.getParameterValues("country");
for(int i=0;i<3;i++)
{
out.println(name[i]);
out.println(place[i]);
out.println(country[i]);
}
I notice you've a wrong condition in your loop.
for(i=1;i>=3;i++) // this i>=3 will return false since i is not greater than or equal to 3.
change the condition of your loop to less than or equal to 3.
for(i=1;i<=3;i++){
name[i]= request.getParameter("name"+i);
place[i] = request.getParameter("place"+i);
country[i] = request.getParameter("country"+i);
/*
it will access the ff.
"name1" to "name3"
"place1" to "place3"
"country1" to "country3"
*/
}
then change your declaration of array size to 4, since your accessing above the index 3.
String[] name = new String[4];
String[] place = new String[4];
String[] country = new String[4];
Also your getting java.lang.ArrayIndexOutOfBoundsException:
because you've accessed an illegal index. Your index is negative or greater than or equal to the size of the array that why it throws ArrayIndexOutOfBoundsException, make sure you access a legal index.
If you have something like this, it will throw an ArrayIndexOutOfBoundsException because you access the equal size of an array;
String[] name = new String[3];
out.println(name[3]); //you can only access 0 - 2 index, thats whay it throws ArrayIndexOutOfBoundsException,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.