Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multidimensional arrays

 
ilteris kaplan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to retrieve some information from a php file and the way I am getting the results is just like this:

432
333 99
17 19 22 31 85 76 51 32
9 17 16 5 13 12 14 13


so what I am doing in processing is call String lines[] = p.loadStrings(url); which gives me every line as an element of lines[] array. now what I should do is to use split function to put each element in every line in an array in order to use them later. so something along this way:

// before constructor
int totalPressed[];
int locationPressed[];
int areaOne[];
int areaTwo[];

// I am not even sure if I can do this.
int [][] metArray = {totalPressed, locationPressed, areaOne, areaTwo};




public void parsePHP(String url) {
String lines[] = p.loadStrings(url);
for (int i=0; i < lines.length; i++) {
// I need to find a way to
// push those values into those arrays.
// trying to declare below which doesn't work.
metArray[i][] = int(PApplet.split(lines[i]));
}
}


OF course this is not working
can you tell me what I am missing here?

thanks!
ilteris.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without trying it, all that looks really close except that last line that does all the work.

You won't be able to split a String into an array of ints in one shot, but you can split it into an array of Strings, then manually build the array of ints.

Are you comfortable making an int from a String? If not, read up on the Integer class to find a method that will do the job.

Does that seem about right?
 
ilteris kaplan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick response, yep your code made aa lot more sense, and here is what I came up with!



right now the problem is when I try to declare the arrays here:
int metArray[i][] = new int[strs.length];

since I don't have a second indexer, what should I be using here instead?
Am I on the right track?

thanks a lot
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm following you, metArray needs to have one element in the first dimension for each line. So outside the first loop you can declare

int[][] metArray = new int[lines.length][];

The 2nd dimension size is unknown and will vary per line. What you find in each element in the first dimension is another array:

metArray[i] = new int[strs.length];

Now your line in the inner loop per token is good:

metArray[i][j] = Integer.parseInt(strs[j]);

The main thing to pick up is that metArray[i] is another array. Did that make sense?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic