【发布时间】:2019-04-23 17:27:30
【问题描述】:
我正在尝试获取矩阵的输入,这是来自 JSP 页面的多维数组并打印它。 我尽力了,
索引.JSP
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>TSP</title>
</head>
<body>
<h1> Matrix</h1>
<form action="tsp.jsp">
<label>No of cities</label>
<input type="text" name="cities">
<label>Enter matrix</label>
<input type="text" name="matrix">
<button type="submit">Submit</button>
</form>
</body>
</html>
TSP.JSP
<%--
Created by IntelliJ IDEA.
User: Abhishek
Date: 11/21/2018
Time: 12:01 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Display</title>
</head>
<body>
<% int city= Integer.parseInt(request.getParameter("cities"));
int matrix[][]=new int[100][100];
for ( int i=0; i<city;i++) {
for (int j = 0; j < city; j++) {
matrix[i][j]= Integer.parseInt(request.getParameter("matrix"));
}
}
%>
<% out.print(city);
for ( int i=0; i<city;i++) {
for (int j = 0; j < city; j++) {
out.print(matrix);
}
}
%>
</body>
</html>
当我输入值 city 为 4 时,matrix = 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4 并单击提交显示异常:
java.lang.NumberFormatException:对于输入字符串:“1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4”
这样做的目的基本上是,如果成功,我想将这些传递给 java 类来解决旅行商问题。该程序运行良好。我想为它创建一个网络界面,但我被困在这一点上。
【问题讨论】:
-
@Torgeist,谢谢我编辑并更改了它。但后来我也得到了上述异常。帮助我完成这个,因为这对我当前的项目非常重要。谢谢。
-
您在
index.jsp中为city和matrix输入了哪些值?这是NumberFormatException。 -
城市 =4。所以对于 4x4 矩阵,我输入了 matrix = 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4
-
在桌面 Java 程序中尝试此代码并查看结果:
Integer.parseInt("1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4") -
显示相同的异常。
标签: java arrays jsp servlets jakarta-ee