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;
}
}
}
str = str1[i][0].split(",");you should switch the indices by usingstr = str1[0][i].split(",");"1,2,3,4,5"possible? If so it be converted to{1,2,3,4}or{1,2,3,4,5}?