我正在制作一个包含String和Integer变量的变量形式。
即我从page1.jsp页面获取的参数是name1,place1,country1,name2,place2,country2,name3,place3,country3。
我想获取这些变量并在page2.jsp上打印。
到目前为止,这是我尝试过的。
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));
}
我得到的错误是:
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
我发现您的循环条件不正确。
for(i=1;i>=3;i++) // this i>=3 will return false since i is not greater than or equal to 3.
将循环条件更改为小于或等于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"
*/
}
然后将数组大小的声明更改为4,因为您在索引3上方进行访问。
String[] name = new String[4];
String[] place = new String[4];
String[] country = new String[4];
这也是您获得的,java.lang.ArrayIndexOutOfBoundsException:
因为您访问了非法索引。您的索引为负数或大于或等于为什么它引发ArrayIndexOutOfBoundsException的数组的大小,请确保您访问合法的索引。
如果您有类似的东西,它将访问ArrayIndexOutOfBoundsException,因为您访问的数组大小相等。
String[] name = new String[3];
out.println(name[3]); //you can only access 0 - 2 index, thats whay it throws ArrayIndexOutOfBoundsException,
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句