Can a table be created (a csv file) from out of a class instead out of "void setup()" ?

Nothing seems to be written when I look at the csv file in question, no columns are created. But I see no error messages either.

Answers

  • I'm not sure what your question means. Can you post an MCVE that demonstrates what you're talking about?

  • edited October 2014

    Sure!

    CSVPreparation InsTCSVPreparation = new CSVPreparation(0);
    
    void setup()
    {
    InsTCSVPreparation.StartCSV ();
    }
    
    class CSVPreparation
    {
    int val;
    CSVPreparation( int tempval )
    {val = tempval;}
    
    void StartCSV ()
    {
    
    storeData = new Table(); 
    
    storeData.addColumn("A");
    storeData.addColumn("B")
    
    storeData.setString( 1, "A",  "Kether");  
    storeData.setString( 1, "B", "Binnah");  
    }// f
    }// C
    
  • Word to the wise: you should try to use standard naming conventions: variables and methods should start with lower-case letters.

    Does this code work? What do you expect it to do? What does it do instead? I'm still not sure what your question is...

  • edited October 2014

    It should write a CSV file, thats all. But it doesn't.

  • Where is your storeData variable declared? Does this code compile?

  • edited October 2014 Answer ✓

    Define a class to merely create a Table is a little too much.
    A simpler createCSV() util function is enough IMO: (*)

    http://forum.processing.org/two/discussion/2801/picking-cards-at-random-then-excluding-those-from-further-picking-

    // forum.processing.org/two/discussion/7733/
    // can-a-table-be-created-a-csv-file-
    // from-out-of-a-class-instead-out-of-void-setup-
    
    final Table table = createCSV();
    
    void setup() {
      printTable(table);
      saveTable(table, dataPath("MyTable.csv"));
      exit();
    }
    
    static final Table createCSV() {
      Table t = new Table();
    
      //t.addColumn("A");
      //t.addColumn("B");
    
      t.setString(0, "A", "Kether");
      t.setString(0, "B", "Binnah");
    
      t.setString(1, "A", "Frodo");
      t.setString(1, "B", "Chun-Li");
    
      return t;
    }
    
    static final void printTable(Table t) {
      String[] titles = new String[t.getColumnCount()];
    
      for (int i = 0; i != titles.length
        ; titles[i] = t.getColumnTitle(i++));
    
      println(titles);
      println();
    
      int i = 0;
      for (TableRow r : t.rows()) {
        print("[" + i++ + "] - | ");
    
        for (String s : titles)
          print(s + " : " + r.getString(s) + " | ");
    
        println();
      }
    }
    
  • Thx!, thought the reason that I was using a class was because I thought that at all times you have to revert to classes. But it seems that this is not always the case.. (?)

  • Actually, a Processing's sketch is itself a Java class! ;)

  • Well, it all works now as intended. Thank you very much ;)

Sign In or Register to comment.