简体   繁体   中英

Convert String into 2D int array

I have problem with conversion from String into two dimension int array.
Let's say I have:

String x = "1,2,3;4,5,6;7,8,9"

(In my program it will be String from text area.) and I want to create array nxn

int[3][3] y = {{1,2,3},{4,5,6},{7,8,9}} 

(Necessary for next stages.) I try to split the string and create 1 dimensional array, but I don't have any good idea what to do next.


As you suggest I try split at first using ; then , but my solution isn't great. It works only when there will be 3 x 3 table. How to create a loop making String arrays?

public int[][] RunMSTFromTextFile(JTextArea ta)
    {
        String p = ta.getText();
        String[] tp = p.split(";");

        String tpA[] = tp[0].split(",");
        String tpB[] = tp[1].split(",");
        String tpC[] = tp[2].split(",");

        String tpD[][] = {tpA, tpB, tpC};

        int matrix[][] = new int[tpD.length][tpD.length];

        for(int i=0;i<tpD.length;i++)
        {
            for(int j=0;j<tpD.length;j++)
            {
                matrix[i][j] = Integer.parseInt(tpD[i][j]);
            }
        }
        return matrix;
    }
  1. Split by ; to get rows.
  2. Loop them, incrementing a counter (eg x )
    1. Split by , to get values of each row.
    2. Loop those values, incrementing a counter (eg y )
      • Parse each value (eg using one of the parseInt methods of Integer ) and add it to the x,y of the array.

If you have already created an int[9] and want to split it into int[3][3] :

for (int i = 0; i < 3; i++) {
  for (int j = 0; j < 3; j++) {
    toArray[i][j] = fromArray[(3*i) + j);
  }
}

Now, if the 2-dimensional array is not rectangular, ie the size of inner array is not same for all outer arrays, then you need more work. You would do best to use a Scanner and switch between nextString and next . The biggest challenge will be that you will not know the number of elements (columns) in each row until you reach the row-terminating semi-colon

After using split, take a look at Integer.parseInt() to get the numbers out.

String lines[] = input.split(";");
int width = lines.length;
String cells[] = lines[0].split(",");
int height = cells.length;
int output[][] = new int[width][height];

for (int i=0; i<width; i++) {
    String cells[] = lines[i].split(",");
    for(int j=0; j<height; j++) {
        cells[i][j] = Integer.parseInt(cells[j]);
    }
}

Then you need to decide what to do with NumberFormatExceptions

A solution using 2 splits:

String input = "1,2,3;4,5,6;7,8,9";

String[] x = input.split(";");

String[][] result = new String[x.length][];
for (int i = 0; i<x.length; i++) {
    result[i] = x[i].split(",");
}

This give a 2 dimension array of strings you will need to parse those ints afterwards, it depends on the use you want for those numbers. The following solution shows how to parse them as you build the result:

String input = "1,2,3;4,5,6;7,8,9";

String[] x = input.split(";");

int[][] result = new int[x.length][];

for (int i = 0; i < x.length; i++) {
    String[] row = x[i].split(",");
    result[i] = new int[row.length];

    for(int j=0; j < row.length; j++) {
        result[i][j] = Integer.parseInt(row[j]);
    }
}

Super simple method!!!

package ADVANCED;
import java.util.Arrays;
import java.util.Scanner;

public class p9 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);

        String x=sc.nextLine();
        String[] array = x.split(",");
        int length_x=array.length;
        int[][] two=new int[length_x/2][2];

        for (int i = 0; i <= length_x-1; i=i+2) {
            two[i/2][0] = Integer.parseInt(array[i]);
        }

        for (int i = 1; i <= length_x-1; i=i+2) {
            two[i/2][1] = Integer.parseInt(array[i]);
        }   
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM