2

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 n x n

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;
    }
3
  • you must use split by ; and then for each entry in the array split by , Commented Jan 24, 2013 at 22:51
  • Using split as you say sounds like an idea that could go further. Commented Jan 24, 2013 at 22:51
  • when I use only comma for spliting: ' public int[] RunMSTFromTextFile(JTextArea ta) { String p = ta.getText(); String[] tp = p.split(","); int matrix[] = new int[tp.length]; for(int i=0;i<tp.length;i++) { matrix[i] = Integer.parseInt(tp[i]); } return matrix; } Commented Jan 24, 2013 at 23:00

5 Answers 5

3

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++) {
        output[i][j] = Integer.parseInt(cells[j]);
    }
}

Then you need to decide what to do with NumberFormatExceptions

Sign up to request clarification or add additional context in comments.

2 Comments

I believe you meant to say String cells[].
Just converted to C# and fixed a problem for me. Thanks!
3
  1. Split by ; to get rows.
  2. Loop them, incrementing a counter (e.g. x)
    1. Split by , to get values of each row.
    2. Loop those values, incrementing a counter (e.g. y)
      • Parse each value (e.g. using one of the parseInt methods of Integer) and add it to the x,y of the array.

1 Comment

+1 leading without spoonfeeding. Probably should include the name of the parse method.
2

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, i.e. 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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.