We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › checking elements of an array
Page Index Toggle Pages: 1
checking elements of an array (Read 987 times)
checking elements of an array
Feb 5th, 2010, 12:06pm
 
Hi,
importing .txt file data and want to draw nodes representing each imput.
I started using the code in Processing handbook (loading characters, p431) so i understand that as not all entries are same length i need to pretend by 'tabbing' so processing reads the file. Some lines have more entries, so i just tabbed and typed a "." to even lengths. The code below works and represents each entry with a node but i don't want to represent the "." entries. How can i do this please?

Code is:
Record[] records;
int recordCount;

void setup() {
 size(300,300);
 noStroke();
 fill(250);
 background(0);
 smooth();

 String[]lines = loadStrings("familyMap.txt");
 records = new Record[lines.length];
 for (int i = 0; i < lines.length; i++) {
   String[] pieces = split(lines[i], '\t'); // Load data into array
   if (pieces.length > 1) {
     records[recordCount] = new Record(pieces);
     recordCount++;
   }
 }
 for (int i = 0; i < recordCount; i++) {
   // draw node at random location to represent parent
   float x = random(5, width-5);            
   float y = random(5, height-5);  
   ellipse(x,y,20,20);
   // Print to console: check
   println(i+1 + ": " + records[i].parent +  " - " + records[i].child1 +  " - " + records[i].child2 +  " - " + records[i].child3 +  " - " + records[i].child4 +  " - " + records[i].child5);
   for (int j = 0; j < lines.length; j++) {
     // draw node at random location to represent children
     float x1 = random(5, width-5);            
     float y1 = random(5, height-5);    
     ellipse(x1,y1,8,8);  
   }
 }
}
Re: checking elements of an array
Reply #1 - Feb 5th, 2010, 12:35pm
 
I don't get the part with the dots. What are they supposed to do? What does your file looks like?
Re: checking elements of an array
Reply #2 - Feb 6th, 2010, 5:20am
 
my text file is like a family tree so i have a number of parents - a line which first entry is a parent and after that a number of children entries; which are seprerated with tabs.
The problem is that each parent has a different number of children. One parent has five children another three, so the entry for the latter has 2 entries which are just tabs (empty). I marked these with a "." thinking i could use the symbol "." to identify it and so say if entry = "." ignore it.

maybe i am structuring the file wrong? I thought it 'easier' to have each line with parent as first entry and children after seperated by tabs.
Re: checking elements of an array
Reply #3 - Feb 6th, 2010, 7:13am
 
OK.
When you do: new Record(pieces), you give an array with the entries, real or dummy.
Not sure what is in Record constructor, but you can iterate on the array and eliminate the dot entries (length of string below 2, for example).
Re: checking elements of an array
Reply #4 - Feb 6th, 2010, 8:15am
 
Thanks for the help PhiLho but i am not sure how to "iterate on the array and eliminate the dot entries".
Sorry if i am being a simpleton but could you explain for me please. Thankyou.

believe i need to include dummies to make all entries equal length (being length of parent with most children) otherwsie i get an error;
ArrayIndexOutOfBoundsException:

here is the constructor - sorry not included before.
class Record {
 String parent;
 String child1;
 String child2;
 String child3;
 String child4;
 String child5;

 public Record(String[]pieces) {
   parent = pieces[0];
   child1 = pieces[1];
   child2 = pieces[2];
   child3 = pieces[3];
   child4 = pieces[4];
   child5 = pieces[5];
 }
}
Re: checking elements of an array
Reply #5 - Feb 6th, 2010, 8:31am
 
Well, perhaps you can change the class to:
Code:
class Record {
String parent;
String[] children;

public Record(String[] pieces) {
parent = pieces[0];
children = new String[pieces.length - 1];
int childNb = 0;
for (int i = 0; i < children.length; i++) {
if (pieces[i + 1].length > 1) {
children[childNb++] = pieces[i + 1];
}
}
children = subset(children, 0, childNb);
}
}
(untested)
Page Index Toggle Pages: 1