Taking input to a matrix from JSP textbox?
I'm trying to take input for a matrix , that is a multidimensional array from JSP page and print it.
I tried what i can,
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>
When i enter the values city as 4 , matrix = 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4 and click submit it shows the exception:
java.lang.NumberFormatException: For input string: "1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4"
The purpose for this is basically, if this becomes successful i want to pass these to java class to solve the Travelling Salesman Problem. The program is running fine. I wanted create a web interface for it and i'm stuck at this point.
java arrays jsp servlets java-ee
|
show 9 more comments
I'm trying to take input for a matrix , that is a multidimensional array from JSP page and print it.
I tried what i can,
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>
When i enter the values city as 4 , matrix = 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4 and click submit it shows the exception:
java.lang.NumberFormatException: For input string: "1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4"
The purpose for this is basically, if this becomes successful i want to pass these to java class to solve the Travelling Salesman Problem. The program is running fine. I wanted create a web interface for it and i'm stuck at this point.
java arrays jsp servlets java-ee
@Torgeist, thanks I edited and changed it. But then also i'm getting the above mentioned exception. Help me through this, because this is very important for my current project. Thank You.
– Abhishek
Nov 21 at 7:31
What are the values you have entered for thecity
andmatrix
in theindex.jsp
? This is NumberFormatException.
– prasad_
Nov 21 at 7:37
city =4. So for 4x4 matrix i entered matrix = 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4
– Abhishek
Nov 21 at 7:49
Try this code in a desktop Java program and see the result:Integer.parseInt("1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4")
– prasad_
Nov 21 at 7:53
Showing the same exception.
– Abhishek
Nov 21 at 7:57
|
show 9 more comments
I'm trying to take input for a matrix , that is a multidimensional array from JSP page and print it.
I tried what i can,
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>
When i enter the values city as 4 , matrix = 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4 and click submit it shows the exception:
java.lang.NumberFormatException: For input string: "1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4"
The purpose for this is basically, if this becomes successful i want to pass these to java class to solve the Travelling Salesman Problem. The program is running fine. I wanted create a web interface for it and i'm stuck at this point.
java arrays jsp servlets java-ee
I'm trying to take input for a matrix , that is a multidimensional array from JSP page and print it.
I tried what i can,
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>
When i enter the values city as 4 , matrix = 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4 and click submit it shows the exception:
java.lang.NumberFormatException: For input string: "1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4"
The purpose for this is basically, if this becomes successful i want to pass these to java class to solve the Travelling Salesman Problem. The program is running fine. I wanted create a web interface for it and i'm stuck at this point.
java arrays jsp servlets java-ee
java arrays jsp servlets java-ee
edited Nov 21 at 7:49
asked Nov 21 at 7:22
Abhishek
198
198
@Torgeist, thanks I edited and changed it. But then also i'm getting the above mentioned exception. Help me through this, because this is very important for my current project. Thank You.
– Abhishek
Nov 21 at 7:31
What are the values you have entered for thecity
andmatrix
in theindex.jsp
? This is NumberFormatException.
– prasad_
Nov 21 at 7:37
city =4. So for 4x4 matrix i entered matrix = 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4
– Abhishek
Nov 21 at 7:49
Try this code in a desktop Java program and see the result:Integer.parseInt("1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4")
– prasad_
Nov 21 at 7:53
Showing the same exception.
– Abhishek
Nov 21 at 7:57
|
show 9 more comments
@Torgeist, thanks I edited and changed it. But then also i'm getting the above mentioned exception. Help me through this, because this is very important for my current project. Thank You.
– Abhishek
Nov 21 at 7:31
What are the values you have entered for thecity
andmatrix
in theindex.jsp
? This is NumberFormatException.
– prasad_
Nov 21 at 7:37
city =4. So for 4x4 matrix i entered matrix = 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4
– Abhishek
Nov 21 at 7:49
Try this code in a desktop Java program and see the result:Integer.parseInt("1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4")
– prasad_
Nov 21 at 7:53
Showing the same exception.
– Abhishek
Nov 21 at 7:57
@Torgeist, thanks I edited and changed it. But then also i'm getting the above mentioned exception. Help me through this, because this is very important for my current project. Thank You.
– Abhishek
Nov 21 at 7:31
@Torgeist, thanks I edited and changed it. But then also i'm getting the above mentioned exception. Help me through this, because this is very important for my current project. Thank You.
– Abhishek
Nov 21 at 7:31
What are the values you have entered for the
city
and matrix
in the index.jsp
? This is NumberFormatException.– prasad_
Nov 21 at 7:37
What are the values you have entered for the
city
and matrix
in the index.jsp
? This is NumberFormatException.– prasad_
Nov 21 at 7:37
city =4. So for 4x4 matrix i entered matrix = 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4
– Abhishek
Nov 21 at 7:49
city =4. So for 4x4 matrix i entered matrix = 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4
– Abhishek
Nov 21 at 7:49
Try this code in a desktop Java program and see the result:
Integer.parseInt("1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4")
– prasad_
Nov 21 at 7:53
Try this code in a desktop Java program and see the result:
Integer.parseInt("1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4")
– prasad_
Nov 21 at 7:53
Showing the same exception.
– Abhishek
Nov 21 at 7:57
Showing the same exception.
– Abhishek
Nov 21 at 7:57
|
show 9 more comments
3 Answers
3
active
oldest
votes
You can try this code:
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]));
}
}
}
The Output:
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]
Thank you @prasad_ as your suggestions i did the coding part and answered below.
– Abhishek
Nov 21 at 10:42
add a comment |
And at last, this is working fine.
This will accept array of string and then convert it into integer and store it in an array.
<%@ 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>
The only thing is, it's not printing in matrix format. Any suggestions are accepted.
You can use (1) HTML table with rows and columns to print the matrix, and (2) JSP JSTLforEach
tag to print from the array. Search the net to get some examples of how to use those, individually.
– prasad_
Nov 21 at 10:55
See this post: Print 2d Array in HTML Table.
– prasad_
Nov 21 at 11:04
Yeah sure.But it's not necessary for my project. I'm giving this 2d array as a input to my Java code for solving Travelling Salesman Problem. Thank you for guiding me.
– Abhishek
Nov 21 at 11:08
add a comment |
You are missing something. After one row printing, you need to move to the next row.
for(int i=0; i<array2d.length;i++) {
for(int j=0; j<array2d[i].length;j++) {
out.print(array2d[i][j]+" ");
} out.println();
}
It's already in my code. Have a look properly.
– Abhishek
Nov 21 at 11:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407067%2ftaking-input-to-a-matrix-from-jsp-textbox%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try this code:
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]));
}
}
}
The Output:
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]
Thank you @prasad_ as your suggestions i did the coding part and answered below.
– Abhishek
Nov 21 at 10:42
add a comment |
You can try this code:
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]));
}
}
}
The Output:
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]
Thank you @prasad_ as your suggestions i did the coding part and answered below.
– Abhishek
Nov 21 at 10:42
add a comment |
You can try this code:
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]));
}
}
}
The Output:
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]
You can try this code:
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]));
}
}
}
The Output:
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]
edited Nov 21 at 9:56
answered Nov 21 at 9:39
prasad_
1,2791515
1,2791515
Thank you @prasad_ as your suggestions i did the coding part and answered below.
– Abhishek
Nov 21 at 10:42
add a comment |
Thank you @prasad_ as your suggestions i did the coding part and answered below.
– Abhishek
Nov 21 at 10:42
Thank you @prasad_ as your suggestions i did the coding part and answered below.
– Abhishek
Nov 21 at 10:42
Thank you @prasad_ as your suggestions i did the coding part and answered below.
– Abhishek
Nov 21 at 10:42
add a comment |
And at last, this is working fine.
This will accept array of string and then convert it into integer and store it in an array.
<%@ 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>
The only thing is, it's not printing in matrix format. Any suggestions are accepted.
You can use (1) HTML table with rows and columns to print the matrix, and (2) JSP JSTLforEach
tag to print from the array. Search the net to get some examples of how to use those, individually.
– prasad_
Nov 21 at 10:55
See this post: Print 2d Array in HTML Table.
– prasad_
Nov 21 at 11:04
Yeah sure.But it's not necessary for my project. I'm giving this 2d array as a input to my Java code for solving Travelling Salesman Problem. Thank you for guiding me.
– Abhishek
Nov 21 at 11:08
add a comment |
And at last, this is working fine.
This will accept array of string and then convert it into integer and store it in an array.
<%@ 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>
The only thing is, it's not printing in matrix format. Any suggestions are accepted.
You can use (1) HTML table with rows and columns to print the matrix, and (2) JSP JSTLforEach
tag to print from the array. Search the net to get some examples of how to use those, individually.
– prasad_
Nov 21 at 10:55
See this post: Print 2d Array in HTML Table.
– prasad_
Nov 21 at 11:04
Yeah sure.But it's not necessary for my project. I'm giving this 2d array as a input to my Java code for solving Travelling Salesman Problem. Thank you for guiding me.
– Abhishek
Nov 21 at 11:08
add a comment |
And at last, this is working fine.
This will accept array of string and then convert it into integer and store it in an array.
<%@ 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>
The only thing is, it's not printing in matrix format. Any suggestions are accepted.
And at last, this is working fine.
This will accept array of string and then convert it into integer and store it in an array.
<%@ 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>
The only thing is, it's not printing in matrix format. Any suggestions are accepted.
answered Nov 21 at 10:41
Abhishek
198
198
You can use (1) HTML table with rows and columns to print the matrix, and (2) JSP JSTLforEach
tag to print from the array. Search the net to get some examples of how to use those, individually.
– prasad_
Nov 21 at 10:55
See this post: Print 2d Array in HTML Table.
– prasad_
Nov 21 at 11:04
Yeah sure.But it's not necessary for my project. I'm giving this 2d array as a input to my Java code for solving Travelling Salesman Problem. Thank you for guiding me.
– Abhishek
Nov 21 at 11:08
add a comment |
You can use (1) HTML table with rows and columns to print the matrix, and (2) JSP JSTLforEach
tag to print from the array. Search the net to get some examples of how to use those, individually.
– prasad_
Nov 21 at 10:55
See this post: Print 2d Array in HTML Table.
– prasad_
Nov 21 at 11:04
Yeah sure.But it's not necessary for my project. I'm giving this 2d array as a input to my Java code for solving Travelling Salesman Problem. Thank you for guiding me.
– Abhishek
Nov 21 at 11:08
You can use (1) HTML table with rows and columns to print the matrix, and (2) JSP JSTL
forEach
tag to print from the array. Search the net to get some examples of how to use those, individually.– prasad_
Nov 21 at 10:55
You can use (1) HTML table with rows and columns to print the matrix, and (2) JSP JSTL
forEach
tag to print from the array. Search the net to get some examples of how to use those, individually.– prasad_
Nov 21 at 10:55
See this post: Print 2d Array in HTML Table.
– prasad_
Nov 21 at 11:04
See this post: Print 2d Array in HTML Table.
– prasad_
Nov 21 at 11:04
Yeah sure.But it's not necessary for my project. I'm giving this 2d array as a input to my Java code for solving Travelling Salesman Problem. Thank you for guiding me.
– Abhishek
Nov 21 at 11:08
Yeah sure.But it's not necessary for my project. I'm giving this 2d array as a input to my Java code for solving Travelling Salesman Problem. Thank you for guiding me.
– Abhishek
Nov 21 at 11:08
add a comment |
You are missing something. After one row printing, you need to move to the next row.
for(int i=0; i<array2d.length;i++) {
for(int j=0; j<array2d[i].length;j++) {
out.print(array2d[i][j]+" ");
} out.println();
}
It's already in my code. Have a look properly.
– Abhishek
Nov 21 at 11:06
add a comment |
You are missing something. After one row printing, you need to move to the next row.
for(int i=0; i<array2d.length;i++) {
for(int j=0; j<array2d[i].length;j++) {
out.print(array2d[i][j]+" ");
} out.println();
}
It's already in my code. Have a look properly.
– Abhishek
Nov 21 at 11:06
add a comment |
You are missing something. After one row printing, you need to move to the next row.
for(int i=0; i<array2d.length;i++) {
for(int j=0; j<array2d[i].length;j++) {
out.print(array2d[i][j]+" ");
} out.println();
}
You are missing something. After one row printing, you need to move to the next row.
for(int i=0; i<array2d.length;i++) {
for(int j=0; j<array2d[i].length;j++) {
out.print(array2d[i][j]+" ");
} out.println();
}
edited Nov 21 at 11:48
Dave
2,84941026
2,84941026
answered Nov 21 at 10:52
Nagaraj S Kharvi
116
116
It's already in my code. Have a look properly.
– Abhishek
Nov 21 at 11:06
add a comment |
It's already in my code. Have a look properly.
– Abhishek
Nov 21 at 11:06
It's already in my code. Have a look properly.
– Abhishek
Nov 21 at 11:06
It's already in my code. Have a look properly.
– Abhishek
Nov 21 at 11:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407067%2ftaking-input-to-a-matrix-from-jsp-textbox%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@Torgeist, thanks I edited and changed it. But then also i'm getting the above mentioned exception. Help me through this, because this is very important for my current project. Thank You.
– Abhishek
Nov 21 at 7:31
What are the values you have entered for the
city
andmatrix
in theindex.jsp
? This is NumberFormatException.– prasad_
Nov 21 at 7:37
city =4. So for 4x4 matrix i entered matrix = 1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4
– Abhishek
Nov 21 at 7:49
Try this code in a desktop Java program and see the result:
Integer.parseInt("1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4")
– prasad_
Nov 21 at 7:53
Showing the same exception.
– Abhishek
Nov 21 at 7:57