0

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.

Abhishek P
  • 58
  • 1
  • 12
  • @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 P Nov 21 '18 at 07:31
  • What are the values you have entered for the `city` and `matrix` in the `index.jsp`? This is [NumberFormatException](https://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html). – prasad_ Nov 21 '18 at 07: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 P Nov 21 '18 at 07: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 '18 at 07:53
  • Showing the same exception. – Abhishek P Nov 21 '18 at 07:57
  • What is it you are trying to assign to this in the JSP: `matrix [i][j] = ...`. – prasad_ Nov 21 '18 at 08:02
  • The values we get from textbox . Look at this https://stackoverflow.com/questions/53358330/taking-input-to-a-java-or-c-program-from-a-web-page you will get an idea of what i'm looking for – Abhishek P Nov 21 '18 at 08:06
  • You can't convert string with spaces into an integer in this format. also request.parameter copy all of the data in the j column each time. the code need to be rewritten and in other format. learn about how to fill int[][] from string. goodluck! – Mohammadreza Khatami Nov 21 '18 at 08:09
  • Thank you @Mohammadreza Khatami. Could you be more specific, or at least guide me like how it can be done? I tried every possible way, but i could not. – Abhishek P Nov 21 '18 at 08:17
  • You may just remove spaces from the string :) – Mohammadreza Khatami Nov 21 '18 at 08:23
  • I think you want to convert this string to 4x4 matrix? `"1 2 3 4 5 6 7 8 8 8 8 8 1 2 3 4"` – prasad_ Nov 21 '18 at 08:42
  • Yes,exactly what i want to – Abhishek P Nov 21 '18 at 08:44
  • (1) Convert the string to a `String []` array - use `String`'s `split()` method to do that. (2) Convert each `String []` array element to an `int` and store in an `int` array of the same size - use the `Integer.parseInt()` to convert string to integer. (3) Convert the one dimensional `int` array to a two dimensional `int` array. I suggest you try these in a desktop program and implement it in the jsp. – prasad_ Nov 21 '18 at 08:51
  • A post showing how to [convert 1d array to 2d array](https://stackoverflow.com/questions/5134555/how-to-convert-a-1d-array-to-2d-array). – prasad_ Nov 21 '18 at 08:57
  • Can someone help me how do I pass the value of row and column dimension to java function which were taken by user in an jsp form – chandni mirchandani Nov 27 '21 at 00:51

3 Answers3

1

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]
prasad_
  • 12,755
  • 2
  • 24
  • 36
0

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.

Abhishek P
  • 58
  • 1
  • 12
  • 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 '18 at 10:55
  • See this post: [Print 2d Array in HTML Table](https://stackoverflow.com/questions/9458217/need-help-using-cforeach-in-jsp-jstl). – prasad_ Nov 21 '18 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 P Nov 21 '18 at 11:08
0

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();
}
Dave
  • 3,073
  • 7
  • 20
  • 33
Nagaraj S Kharvi
  • 73
  • 1
  • 2
  • 12