1

I want to convert array of strings, for example

str1[][] ={{"1,2,3,4","0","2,1"}};

to int array like that:

int arr[][]={{1,2,3,4},{0,0,0,0},{2,1,0,0}} 

Im not able to use just split method, because the size of the tempString changes after every iteration.

My code:

int numOfelements = 3;

        String str1[][] = { { "1,2,3,4", "0", "2,1" } };
        int graph[][] = new int[numOfelements][numOfelements];
        String str[]= new String[numOfelements];


            for (int i = 0; i < numOfelements; ++i) {
                str = str1[i][0].split(",");
                for (int j = 0; j < numOfelements; ++j) {
                    try {
                        if (str.length < j)
                            graph[i][j] = Integer.parseInt(str[i]);
                        else
                            graph[i][j] = 0;
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(new JFrame(), "Error in the data!",
                                "Error", JOptionPane.ERROR_MESSAGE);
                        break;
                    }
                }
            }
4
  • What is the question? Commented Mar 15, 2016 at 22:12
  • I think, instead of str = str1[i][0].split(","); you should switch the indices by using str = str1[0][i].split(","); Commented Mar 15, 2016 at 22:15
  • @Expiredmind Take a look at my solution. Commented Mar 15, 2016 at 22:19
  • Is string like "1,2,3,4,5" possible? If so it be converted to {1,2,3,4} or {1,2,3,4,5}? Commented Mar 15, 2016 at 22:27

2 Answers 2

1

You may write a customized method to fill the array with zeroes. .split() still can be used if you use it cautiously.

public static int[] fillArray(String s, int size){
    String[] tok = str.split(",");
    int[] newArr = new int[size];

    for(int x=0; x<Math.min(size, tok.length); x++)
        newArr[x] = Integer.parseInt(tok[x]);

    for(int x=Math.min(size, tok.length); x<Math.max(size, tok.length); x++)
        newArr[x] = 0;

    return newArr;
}

Get the array data and fill it with zeroes and put it back in a new array:

int[][] arr;
for(int x=0; x<str1[0].length; x++)
    arr[0][x] = fillArray(str1[0][x], 4);  //size of 4 can be changed to your needs
Sign up to request clarification or add additional context in comments.

1 Comment

I must modify your solution a little but it gives me good clue, thanks!
0

try this solution :

int numOfelements = 4;
        String str1[][] = { { "1,2,3,4", "0", "2,1" }};
        int graph[][] = new int[numOfelements][numOfelements];
        String str[]= new String[numOfelements];

        for (int k = 0; k < str1.length; k++) {
            int len =str1[k].length;    
            for (int i = 0; i < len; i++) {
                str = str1[k][i].split(",");    
                for(int j = 0; j < str.length; j++ )
                    graph[i][j]=Integer.parseInt(str[j]);
            }
        }

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.