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 › Newbie Coding Problem
Page Index Toggle Pages: 1
Newbie Coding Problem (Read 1792 times)
Newbie Coding Problem
Nov 15th, 2008, 12:34am
 
Hi all,

I have just started using processing, and am using Ben Fry's"Visualizing Data" book to get to grips with it. However, I am getting an error despite copying the example coding. The program is attempting to map data to an outline of the US.

I am getting the error 'cannot find a class or type named "Table"'

There is probably something blatantly obvious in the code, can anyone help, code below:


PImage mapImage;
Table locationTable;
int rowCount;
Table dataTable;
float dataMin = MAX_FLOAT;
float dataMax = MIN_FLOAT;

void setup() {
 size(640, 400);
 mapImage = loadImage("map.png");
 locationTable = new Table("locations.tsv");
 rowCount = locationTable.getRowCount();
 
 //Read the data table.
 dataTable = new Table("random.tsv");
 
 // Find the minimum and maximum values.
 for (int row = 0; row < rowCount; row++) {
   float value = dataTable.getFloat(row, 1);
   if (value > dataMax) {
     dataMax = value;
   }
   if (value < dataMin) {
     dataMin = value;
   }
 }
}
void draw() {
 background(255);
 image(mapImage, 0, 0);
 
 // Drawing attributes for the ellipses.
 smooth();
 fill(192, 0, 0);
 noStroke();
 for (int row = 0; row < rowCount; row++) {
   String abbrev = dataTable.getRowName(row);
   float x = locationTable.getFloat(abbrev, 1);
   float y = locationTable.getFloat(abbrev, 2);
   drawData(x, y, abbrev);
 }
}

// Map the size of the ellipse to the data value
void drawData(float x, float y, String abbrev) {
 //Get data value for state
 float value = dataTable.getFloat(abbrev, 1);
 // Re-map the value to a number between 2 and 40
 float mapped = map(value, dataMin, dataMax, 2, 40);
 // Draw an ellipse for this item
 ellipse(x, y, mapped, mapped);
}
   //Loop through the rows of the locations file and draw the points.
 for (int row = 0; row < rowCount; row++) {
   float x = locationTable.getFloat(row, 1);  // column 1
   float y = locationTable.getFloat(row, 2);  // column 2
   ellipse(x, y, 9, 9);
 }
}
class Table {
 String[][] data;
 int rowCount;
 
 
 Table() {
   data = new String[10][10];
 }

 
 Table(String filename) {
   String[] rows = loadStrings(filename);
   data = new String[rows.length][];
   
   for (int i = 0; i < rows.length; i++) {
     if (trim(rows[i]).length() == 0) {
       continue; // skip empty rows
     }
     if (rows[i].startsWith("#")) {
       continue;  // skip comment lines
     }
     
     // split the row on the tabs
     String[] pieces = split(rows[i], TAB);
     // copy to the table array
     data[rowCount] = pieces;
     rowCount++;
     
     // this could be done in one fell swoop via:
     //data[rowCount++] = split(rows[i], TAB);
   }
   // resize the 'data' array as necessary
   data = (String[][]) subset(data, 0, rowCount);
 }


 int getRowCount() {
   return rowCount;
 }
 
 
 // find a row by its name, returns -1 if no row found
 int getRowIndex(String name) {
   for (int i = 0; i < rowCount; i++) {
     if (data[i][0].equals(name)) {
       return i;
     }
   }
   println("No row named '" + name + "' was found");
   return -1;
 }
 
 
 String getRowName(int row) {
   return getString(row, 0);
 }


 String getString(int rowIndex, int column) {
   return data[rowIndex][column];
 }

 
 String getString(String rowName, int column) {
   return getString(getRowIndex(rowName), column);
 }

 
 int getInt(String rowName, int column) {
   return parseInt(getString(rowName, column));
 }

 
 int getInt(int rowIndex, int column) {
   return parseInt(getString(rowIndex, column));
 }

 
 float getFloat(String rowName, int column) {
   return parseFloat(getString(rowName, column));
 }

 
 float getFloat(int rowIndex, int column) {
   return parseFloat(getString(rowIndex, column));
 }
 
 
 void setRowName(int row, String what) {
   data[row][0] = what;
 }


 void setString(int rowIndex, int column, String what) {
   data[rowIndex][column] = what;
 }

 
 void setString(String rowName, int column, String what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = what;
 }

 
 void setInt(int rowIndex, int column, int what) {
   data[rowIndex][column] = str(what);
 }

 
 void setInt(String rowName, int column, int what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = str(what);
 }

 
 void setFloat(int rowIndex, int column, float what) {
   data[rowIndex][column] = str(what);
 }


 void setFloat(String rowName, int column, float what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = str(what);
 }
 
 
 // Write this table as a TSV file
 void write(PrintWriter writer) {
   for (int i = 0; i < rowCount; i++) {
     for (int j = 0; j < data[i].length; j++) {
       if (j != 0) {
         writer.print(TAB);
       }
       if (data[i][j] != null) {
         writer.print(data[i][j]);
       }
     }
     writer.println();
   }
   writer.flush();
 }
}

Re: Newbie Coding Problem
Reply #1 - Nov 15th, 2008, 11:06am
 
I haven't seen the book yet, but searching fry book table in the board, I found this topic: append with multidimensional array
Looks like the Table object is defined in another file that you must add to your sketch folder.
Re: Newbie Coding Problem
Reply #2 - Nov 15th, 2008, 1:17pm
 
no, he specifies the Table class in the code example

Code:

class Table {
...


the problem is in the block of code immediately above it - it seems to be missing a line. the brace after the ellipse() matches the one at the start of the method which leaves everything after it orphaned. there needs to be a method declaration on the line below it.

Code:

// Map the size of the ellipse to the data value
void drawData(float x, float y, String abbrev) {
//Get data value for state
float value = dataTable.getFloat(abbrev, 1);
// Re-map the value to a number between 2 and 40
float mapped = map(value, dataMin, dataMax, 2, 40);
// Draw an ellipse for this item
ellipse(x, y, mapped, mapped);
}
//Loop through the rows of the locations file and draw the points.
for (int row = 0; row < rowCount; row++) {
float x = locationTable.getFloat(row, 1); // column 1
float y = locationTable.getFloat(row, 2); // column 2
ellipse(x, y, 9, 9);
}
}
Re: Newbie Coding Problem
Reply #3 - Nov 15th, 2008, 3:15pm
 
Thanks for the help guys - Im baffled as I have checked and rechecked the coding but have followed it to the last letter.

Could you provide any examples of anything I could do to fix it?
Re: Newbie Coding Problem
Reply #4 - Nov 15th, 2008, 5:53pm
 
how can we know what's missing? it could be anything.

i've commented it out and it compiles (which suggests that nothing in the remaining code is looking for a missing function) but given that i don't have map.png or locations.tsv i can't actually run it.

Code:

/*
   //Loop through the rows of the locations file and draw the points.
 for (int row = 0; row < rowCount; row++) {
   float x = locationTable.getFloat(row, 1);  // column 1
   float y = locationTable.getFloat(row, 2);  // column 2
   ellipse(x, y, 9, 9);
 }
}
*/


official errata here:
http://oreilly.com/catalog/9780596514556/errata/
but a quick scan shows nothing (but, again, not knowing page number or example name...)

also there's a board here for bugs in the book:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=WebsiteBugs

(edit, added board link)
Re: Newbie Coding Problem
Reply #5 - Nov 15th, 2008, 9:54pm
 
You can also find the examples here:
http://benfry.com/writing/archives/3
So that you can compare what you've entered so far with the working version.
Re: Newbie Coding Problem
Reply #6 - Nov 18th, 2008, 3:05am
 
I too am working through the examples in Visualizing Data.  

When I was putting mine together, I added the Table class as a separate file.  It looks like you added all of the code for the class at the bottom of your project.  You may want to try taking the code for the Table class out of your project.  After you do that, download the Table class or swipe it from one of the example projects and use Sketch>add File to add it back to your project.  

I was frustrated with adding the Table class too, but did get my map to work.

Looking at one of the example projects from the link that Fry posted will give you an idea of what the pieces will look like once you put them together.  

Good Luck!
Re: Newbie Coding Problem
Reply #7 - Nov 30th, 2008, 4:44pm
 
Hello

I had exactly the same problem as described in this thread although I found it to occur with the Table class defined in a separate file within the sketch.

As for why, I have no idea but I did eventually get it working by removing the Table class and then adding it to the sketch again.

I'm not sure about this being a bug though!

I'm also glad to find Ben Fry's book has a code repository as well.

Martin O'Shea.
Re: Newbie Coding Problem
Reply #8 - Dec 3rd, 2008, 4:50am
 
Hi all,

If there are things that are confusing (or incorrect, for that matter), please let me know--I'd much rather hear what's going wrong and get things fixed for later editions of the book. If you find errors in the book, please feel free to use O'Reilly's online errata system:
http://oreilly.com/catalog/9780596514556/errata/
Or send me email directly (address in on my site) for other issues.
Re: Newbie Coding Problem
Reply #9 - Nov 20th, 2009, 2:20am
 
Hi guys, while working with the same example from the book I encountered the same problem.  

There's nothing wrong with the book code, it's a mistake in our own code. My inexperience with Java bites me in the @SS!

There's an extra (unmatched)   }   somewhere! $%#&%*$
Re: Newbie Coding Problem
Reply #10 - Nov 20th, 2009, 4:14am
 
which example is it?

check here (there could be a typesetting error):
http://benfry.com/writing/archives/3
Page Index Toggle Pages: 1