-4

I have an input String, which has a size of 9 or 16 or 25... (So always a number which has an integer root.)

I need to create a 2 dimensional matrix from it, which I would like to store in a two dimensional array. I know how to store it in a one dimensional array, but I don't know how to upload the two dimensional array with the elements of the string in a correct way.

It would be something like this:

(Let's assume we have 9 characters now)

String Matrix[][] = {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 } };

But the order would be this:

1  2  3
4  5  6
7  8  9

I guess I would use two 2 for loop but not sure about it.

5
  • What is the "correct" order that you are trying to get out of it? Also give an example of an input String.
    – nutfox
    Jul 29, 2015 at 9:06
  • The Matrix variable should be one dimensional
    – mic4ael
    Jul 29, 2015 at 9:08
  • Can you show us the algorithm that you have so far?
    – Timo
    Jul 29, 2015 at 9:11
  • please post a correct input string
    – barun
    Jul 29, 2015 at 9:16
  • I think this last edit is not right. The original showed the input as a one dimensional array of strings, not as the desired output. The questions and the answer are no longer related this way.
    – Timo
    Jul 29, 2015 at 12:30

4 Answers 4

4

A good approach would be calculating the square root of the string length: that way you would find out which is the Matrix sizes, and then split up the string in the "rows" or "columns" you want for your matrix.

int matrixSize = Math.sqrt(input.length());
for(int i = 0; i<matrixSize; i++) {
    for(int j = 0; j<matrixSize; j++){
        matrix[i][j] = input[j];
        j++;
    }
}

Swap i and j in the matrix to switch rows for columns.

Hope this helps.

1

try this

string s="123456789";
int n=new int[3][3],p=0;
for(int i = 0; i<3; i++)
    for(int j = 0; j<3; j++){
        matrix[i][j] =Integer.parseInt(s[p]);
        P++;
    }

you may edit dimensions of 2-d array accordingly.

0
String input = "1,2,3,4,5,6,7,8,9";
String[] numbers = input.split(",");

int size = (int) Math.sqrt(numbers.length);

String[][] matrix = new String[size][size];

for (int i = 0; i < size; ++i) {
    for (int j = 0; j < size; ++j) {
        matrix[i][j] = numbers[i * size + j];
    }
}

for (int i = 0; i < size; ++i) {
    for (int j = 0; j < size; ++j) {
        System.out.print(matrix[i][j] + " ");
    }

    System.out.println();
}
0

Lets guess your input will be like :

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

Then you can do something like :

    String[] inputArray=input.replace("\"", "").split(",");
    for (int i = 0; i < inputArray.length; i++) {
        System.out.println(inputArray[i]);
    }

    int matrixSize=(int) Math.ceil(Math.sqrt(inputArray.length));

    int i,j;
    int index=0;
    for(i=0;i<matrixSize;i++)
    {
        for(j=0;j<matrixSize && index<inputArray.length;j++,index++)
            System.out.print(inputArray[index]);
        System.out.println("");
    }

It will work for any number of input.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.