Abhishek P
Abhishek P

Reputation: 58

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.

Upvotes: 0

Views: 672

Answers (3)

Nagaraj S Kharvi
Nagaraj S Kharvi

Reputation: 73

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();
}

Upvotes: 0

Abhishek P
Abhishek P

Reputation: 58

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.

Upvotes: 0

prasad_
prasad_

Reputation: 14287

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]

Upvotes: 1

Related Questions