Problem Create a Table -

Hello Geniuses,

I’m trying to save data in a table inside a class

Table results = new Table();
results.addColumn("Diameter");
results.addColumn("Left Speed");
results.addColumn("Right Speed");

When i run the program i always get this message
unexpected token: (
and it shades this line
results.addColumn("Diameter");

just to check:
without any class things… this works:

Table results = new Table();
results.addColumn("Diameter");
results.addColumn("Left Speed");
results.addColumn("Right Speed");
TableRow newRow = results.addRow();
  newRow.setInt("Diameter", 1);
  newRow.setFloat("Left Speed", 2);
  newRow.setFloat("Right Speed", 3);
saveTable(results, "data/results.csv");
exit();

/* creates dir and file
data/results.csv
content:

Diameter,Left Speed,Right Speed
1,2.0,3.0

*/

so might be required that you post little bit more of your code.

ok for a class setup try:

class MyTable {
  Table results;
  MyTable() {
    this.results = results;
    results = new Table();
    results.addColumn("Diameter");
    results.addColumn("Left Speed");
    results.addColumn("Right Speed");
  }
  void add(int i, float j, float k ) {
    TableRow newRow = results.addRow();
    newRow.setInt("Diameter", i);
    newRow.setFloat("Left Speed", j);
    newRow.setFloat("Right Speed", k);
  }
  void save() {
    saveTable(results, "data/results.csv");
  }
}

MyTable mytable=new MyTable();

void setup() {
  mytable.add(4, 5, 6);
  mytable.save();
  exit();
}

/*
 data/results.csv
 content:
 
 Diameter,Left Speed,Right Speed
 4,5.0,6.0
 
 
 */


Thanks … That’s all my code


class Robot {
  float diameter;
  float distance;
  int   index = 1;  //index for number of outputs
  int condition;    //if (Nl == Nr or Nl = -Nr) = 0; 
  
  float [] velocityLeft  = {1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0, -0.1, -0.2, -0.3, -0.4, -0.5, -0.6, -0.7, -0.8, -0.9, -1.0};
  float [] velocityRight = {1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0, -0.1, -0.2, -0.3, -0.4, -0.5, -0.6, -0.7, -0.8, -0.9, -1.0};
  
  Table results = new Table();
  results.addColumn("Diameter");
  results.addColumn("Left Speed");
  results.addColumn("Right Speed");
  
  float Nl;    //Right wheel velocity
  float Nr;    //Left  wheel velocity
  float theta = radians(0); //Angle with -x-axis
  
  float firstLocationX;
  float firstLocationY;

  float radius = 3;  //DDMR wheel radius
  float L = 6;       //Distance between two wheels
  
  float vx;
  float vy;
  PVector velocity = new PVector(vx, vy); //linear velocity
  PVector location; //DDMR current location
  float omega;      //Angular velocity
  
  float initialX;
  float initialY;
  
  Robot (){
    initialX = 300;
    initialY = 300;
    location = new PVector (initialX, initialY); // Initialize the location.
  }
  
  void display(){
    
    for (int iterationLeft = 0; iterationLeft < velocityLeft.length; iterationLeft ++)
    {
      for (int iterationRight = 0; iterationRight < velocityRight.length; iterationRight ++)
      {
       
        TableRow newRow = results.addRow();
        condition = 1;
        diameter  = 0;
        distance  = 0;
        
        Nl = velocityLeft[iterationLeft];
        Nr = velocityRight[iterationRight];
        omega = radians((radius/(2*L))*(Nl-Nr));
        
        firstLocationX = location.x;
        firstLocationY = location.y;
        
      
        
        if (Nl == Nr) 
          {
            newRow.setFloat("Diameter", 99999);
            newRow.setFloat("Left Speed", Nl);
            newRow.setFloat("Right Speed", Nr);
            
            condition = 0;
            
            index ++;
          } else if (Nl + Nr == 0) 
          {
            newRow.setFloat("Diameter", 0);
            newRow.setFloat("Left Speed", Nl);
            newRow.setFloat("Right Speed", Nr);
            
            condition = 0;
           
            index ++;
          } 
          
          

        while (condition == 1) 
        {
            
            //new location
            location.add(velocity);
            //Calculate velocity 
            velocity.x = (radius/2)*cos(theta)*(Nr + Nl);
            velocity.y = (radius/2)*sin(theta)*(Nr + Nl);
            //new angle
            theta = theta + omega;
          
            
            
            distance = dist(firstLocationX, firstLocationY, location.x, location.y);
            print("Diameter = " + diameter + "  --  " + "Distance = " + distance + "\n");
            
            if (diameter < distance)
              {
                diameter = distance;
              }//end of (if) statement 
            
            if (diameter > distance)  
              {
                newRow.setFloat("Diameter", diameter);
                newRow.setFloat("Left Speed", Nl);
                newRow.setFloat("Right Speed", Nr);
                saveTable(results, "data/new.csv");
                //print(index + " : Diameter= " + diameter + " - Left Speed = " + Nl + " - Right Speed = " + Nr + "\n");
                condition = 0;
                index++;
                velocity.x = 0;
                velocity.y = 0;
              }//end of (if) Statement 
            
        }//end of (While) Loop 
        
     
      }//end (for) iterationRight loop
    }//end (for) iterationLeft loop  
    
  }//end void Display()
    
}//end class Robot