我正在尝试为matrix接受输入,它是JSP页面中的多维数组并打印出来。 我尽力了,
INDEX.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并单击Submit,它显示例外:
java.lang.NumberFormatException:对于输入字符串:“ 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4”
基本上,这样做的目的是,如果成功的话,我想将它们传递给java类以解决Traveling Salesman问题。该程序运行正常。我想为此创建一个Web界面,但现在我陷入了困境。
答案 0 :(得分:1)
您可以尝试以下代码:
public class StringToInt2dArray {
public static void main(String [] args) {
String s = "1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4";
System.out.println("Input string: " + s);
String [] ss = s.split(" ");
System.out.println("Array of strings: " + Arrays.toString(ss));
int [] int1d = new int [ss.length];
for (int i = 0; i < ss.length; i++) {
int1d [i] = Integer.parseInt(ss[i]);
}
System.out.println("Array of ints: " + Arrays.toString(int1d));
int rows = 4;
int cols = 4;
int ints2d [][] = new int [rows][cols]; // need to know the 2d array size
int n = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
ints2d [i][j] = int1d [n++];
}
}
System.out.println("Array of ints in 2D: ");
for (int i = 0; i < ints2d.length; i++) {
System.out.println(Arrays.toString(ints2d [i]));
}
}
}
输出:
Input string: 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4
Array of strings: [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 1, 2, 3, 4]
Array of ints: [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 1, 2, 3, 4]
Array of ints in 2D:
[1, 2, 3, 4]
[5, 6, 7, 8]
[8, 8, 8, 8]
[1, 2, 3, 4]
答案 1 :(得分:0)
最后,这很好。 这将接受字符串数组,然后将其转换为整数并将其存储在数组中。
<%@ 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];
String numbers=request.getParameter("matrix");
String[] splitText = numbers.split(" ");
int[] mat = new int[splitText.length];
for(int i = 0; i < splitText.length; i++)
{
mat[i] = Integer.parseInt(splitText[i]);//parse every element from string to int and store it in array.
}
int array2d[][] = new int[city][city];
int count=0;
for(int i=0; i<city;i++)
{
for(int j=0;j<city;j++) {
if(count==mat.length)
break;
array2d[i][j]=mat[i*city+j];//conversion of 1d array to 2d
count++;
}
}
for(int i=0; i<array2d.length;i++)
{
for(int j=0; j<array2d[i].length;j++)
{
out.print(array2d[i][j]+" ");
}
}
%>
</body>
</html>
唯一的是,它不是以矩阵格式打印。任何建议都可以接受。
答案 2 :(得分:0)
您缺少什么。在打印完一行之后,您需要移至下一行。
while read k v; do echo ${k}_${v}; done<patterns.txt