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

**URL:** <https://discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457>\
**Category:** Coding Questions\
**Created:** [July 2, 2019, 5:42pm UTC](https://discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457 "2019-07-02T17:42:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [July 2, 2019, 5:42pm UTC](https://discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457/1 "2019-07-02T17:42:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 2, 2019, 6:07pm UTC](https://discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457/2 "2019-07-02T18:07:06Z")

</div>

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

> <https://github.com/processing/processing/blob/processing-0269-3.5.3/core/src/processing/data/Table.java#L138-L155>

We may try to turn a String into a StringBufferInputStream for it: 📿  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/io/StringBufferInputStream.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/io/StringBufferInputStream.html)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [July 2, 2019, 6:16pm UTC](https://discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457/3 "2019-07-02T18:16:17Z")

</div>

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

> The type StringBufferInputStream is deprecated

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 2, 2019, 6:26pm UTC](https://discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457/4 "2019-07-02T18:26:27Z")

</div>

> [@jeremydouglass](#):
>
> > The type StringBufferInputStream is deprecated

Deprecation isn’t an error! 😑

> [@jeremydouglass](#):
>
> `table = new Table(new StringBufferInputStream(tableStr));`

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

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

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [July 2, 2019, 6:31pm UTC](https://discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457/5 "2019-07-02T18:31:38Z")

</div>

> [@GoToLoop](#):
>
> Deprecation isn’t an error!

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

> [@GoToLoop](#):
>
> That’s not the signature I referred in my reply

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 2, 2019, 6:36pm UTC](https://discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457/6 "2019-07-02T18:36:02Z")

</div>

> [@jeremydouglass](#):
>
> and displays only that message without leaving the try block.

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

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

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [July 2, 2019, 6:51pm UTC](https://discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457/7 "2019-07-02T18:51:27Z")

</div>

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

Here is a working demo of a String to Table:

```auto
/**
 * 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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 27, 2022, 11:05pm UTC](https://discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457/8 "2022-11-27T23:05:26Z")

</div>

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:

```auto
/**
 * 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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/7/a/7a12ee640e8efc0bf20569456d948ad5d7178f80.png)

`:)`
