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

1 Like