1

Not sure if my mind is just numb or I am just stupid, but for some reason the logic behind this escapes me. Essentially in an android app I am pulling CSV information through a PHP script. That information gets displayed like

value00,value01,value02,value03,value04;value10,value11,value12,value13,value14;value20, etc...

now I want to set up a two dimensional array where thisArray[0][0] = value00, thisArray[1][1] = value11, thisArray[1][4] = value14, etc. The code I have will split by the ";" but I can't figure out how to then split that array into a two dimensional array set up the way I want. This is what I have: (httpMessage is the string containing the above information)

                 String[][] secondSplit;//
             String[] firstSplit;//
             String currvalue;//

             firstSplit = httpMessage.split(";");
             for(int i=0; i<firstSplit.length; i++) {
                 Log.d("EFFs" + Integer.toString(i),firstSplit[i]);
             }

LogCat shows the desired behavior, EFFs0 = line 1, EFFs1 = line 2, just the way I want it. But now, how do I get that second dimension? Also, since I am 100% sure this is a dumb question with an easy answer I'll throw in another, is there an easy way to tell if a string is a number?

eric_spittle
  • 124
  • 3
  • 13

2 Answers2

7

You could do the following:

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

Pretty sure that would work. Let me know if it doesn't! Hope that helps!

awolfe91
  • 1,627
  • 1
  • 11
  • 11
2

If you know how many rows and columns there are you can use the Scanner class like this (recently learned this from this answer):

Scanner sc = new Scanner(input).useDelimiter("[,;]");
final int M = 5;
final int N = 2;
String[][] matrix = new String[M][N];
for (int r = 0; r < M; r++) {
  for (int c = 0; c < N; c++) {
    matrix[r][c] = sc.next();
  }
}

//Just to see that it worked    
for(String[] row : matrix) {
  for(String col : row) {
    System.out.print(col+",");
  }
  System.out.println();
}
Community
  • 1
  • 1
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
  • Thank you for your answer. The other way is easier, and more portable for the place that it is going in, but I appreciate the lesson on another option :) – eric_spittle Nov 27 '12 at 21:28