Use loadTable with variable like String, String[], String[][], StringList?

Does anyone know if you can use loadTable() to generate a Table using a local variable rather than a file as input?

For example if you have CSV-like data stored in a String, or a String[] (per row), or a String[][] (per field), or a StringList (per row)? Really, any of these.

Class Table got some overloaded constructors, like this 1 which gets an InputStream: :nerd_face:

We may try to turn a String into a StringBufferInputStream for it: :prayer_beads:
Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/io/StringBufferInputStream.html

Interesting. Trying to use StringBufferInputStream e.g. in Processing 3.4 stops the sketch on

The type StringBufferInputStream is deprecated

import java.io.StringBufferInputStream;
String tableStr =
  "A"+"\t"+"B"+"\t"+"C" + "\n" +
  "one"+"\t"+"two"+"\t"+"three" + "\n" + 
  "foo"+"\t"+"bar"+"\t"+"baz";
Table table;
void setup() {
  try {
    table = new Table(new StringBufferInputStream(tableStr));
  }
  catch (IOException err) {
  }
  print(table);
}

Deprecation isn’t an error! :expressionless:

That’s not the signature I referred in my reply. It’s lacking a String as 2nd parameter: :face_with_monocle:

public Table(InputStream input, String options) throws IOException {

True, the message text refers to deprecation – but PDE also stops execution and displays only that message without leaving the try block.

That’s probably it. I’ll try an options string.

In my PDE 3.5.3 it crashes inside the try block w/ “No extension specified for this Table”. :bug:

I’ve got no deprecation message. But I always had “Continuously check for errors” turned off here! :stuck_out_tongue_winking_eye:

Great, thanks. Processing 3.4 doesn’t display that error message correctly.

Here is a working demo of a String to Table:

/**
 * String to Table
 * 2019-07 Processing 3.4
 * discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457/6
 */

import java.io.StringBufferInputStream;

Table table;
String tableStr =
  "A"+"\t"+"B"+"\t"+"C" + "\n" +
  "one"+"\t"+"two"+"\t"+"three" + "\n" + 
  "foo"+"\t"+"bar"+"\t"+"baz";

void setup() {
  try {
    table = new Table(new StringBufferInputStream(tableStr), "tsv");
  }
  catch (IOException err) {
  }
  for (TableRow row : table.rows()) {
    for (int col=0; col<row.getColumnCount(); col++) {
      print(row.getString(col), " ");
    }
    println("");
  }
  exit();
}

output:

A B C
one two three
foo bar baz

2 Likes

Hello @jeremydouglass,

I updated code to remove this in Processing 4.01b:

The type StringBufferInputStream is deprecated

I had some fun with it to stimulate my brain:

/**
 * String to Table
 * 2019-07 Processing 3.4
 * discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457/6
 */
 
/**
 * Modified by glv
 * 2022-11-27 Processing 4.01
 */


//Reference:
// https://stackoverflow.com/questions/62241/how-to-convert-a-reader-to-inputstream-and-a-writer-to-outputstream 
 
import java.io.ByteArrayInputStream;

Table table;
String tableStr = "";

void setup() 
  {
  size(620, 300);
  background(0);
  
  textSize(24);
    
// Create some data for table
  for (int row = 0; row<10; row++)
    {
    for (int col = 0; col<10; col++)
      {
      if (col == 0)
        tableStr += row + ",";  
      else if (col == 5)
        tableStr += "Really wide text!" + ",";           
      else
        tableStr += col*10.0 + ",";   
      }
    tableStr += "\n"; 
    }
    
  println(tableStr);      
    
  try 
    {
    table = new Table(new ByteArrayInputStream(tableStr.getBytes("UTF-8")), "csv");
    }
  catch (IOException e) 
    {
    }
  
  // Output to console
  for (TableRow row : table.rows()) 
    {
    for (int col=0; col<row.getColumnCount(); col++) 
      {
      print(row.getString(col), " ");
      }
    println("");
    }
      
  // Outout text to sketch window    
  for (int row=0; row<table.getRowCount(); row++) 
    {
    int x = 10;  
    for (int col=0; col<table.getColumnCount(); col++) 
      {
      int y = row;
      text(table.getString(row, col), x, 30*y+20); // May have to account for different string lengths!
      x += textWidth(table.getString(row, col)) + 10;
      }
    }  
    
  noLoop();
  }

image

:)