# Error when searching through a table

**URL:** https://discourse.processing.org/t/error-when-searching-through-a-table/3118
**Category:** Beginners
**Created:** [August 30, 2018, 3:52pm UTC](https://discourse.processing.org/t/error-when-searching-through-a-table/3118 "2018-08-30T15:52:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kat](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kat](https://discourse.processing.org/u/kat)
#### Post date: [August 30, 2018, 3:52pm UTC](https://discourse.processing.org/t/error-when-searching-through-a-table/3118/1 "2018-08-30T15:52:23Z")

</div>

Hi all,

I’m quite new to Processing and I was hoping someone could help me with this problem I have when searching through a table.

I used the control P5 library to make a search bar where the user types words separated by commas. My intention is for each word the user typed in to be searched through a table which was converted from a csv file (using loadTable()) that I added to the data folder of the sketch. If any of the words that the user typed in exists in the table, I want the category to be printed.

For example, if my spreadsheet looks like this, and the user typed in “Whale,Lizard”, I want it to print “Mammal,Reptile”:

![Animal%20spreadsheet%202](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/ac428f31ba11e5627276578a592ae25467d14ea7.png)

This is my code for the search method:

```auto
void Search() {
  userInput = cp5.get(Textfield.class, "s").getText();
  inputArr = split(userInput, ',');
  screen =2;
  for (String s : inputArr) {
    for (String obj: ingTable.getStringColumn("Name")) {
         for (TableRow row : ingTable.matchRows(s, obj)) {
            items = items + row.getString("Name");
      }
    }
  }
  print(items);
}

```

Every time I run it, I get this error:

controlP5.ControlBroadcaster printMethodError  
SEVERE: An error occured while forwarding a Controller event, please check your code at Search

Could someone please explain what’s wrong with my search method?  
Thanks!!

---

<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: [August 30, 2018, 4:17pm UTC](https://discourse.processing.org/t/error-when-searching-through-a-table/3118/2 "2018-08-30T16:17:35Z")

</div>

Your table seems confusing to me. 😕  
Shouldn’t it be something like this 1 below instead? 🤔

| Mammal | Insect | Reptile |
| --- | --- | --- |
| Whale | Fly | Snake |
| Zebra | Bee | Lizard |
| Mouse | Ant | Crocodile |

## “Animals.csv”:

```auto
Mammal,Insect,Reptile
Whale,Fly,Snake
Zebra,Bee,Lizard
Mouse,Ant,Crocodile

```

---

<div class="post-metadata">

### Author: ![kat](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kat](https://discourse.processing.org/u/kat)
#### Post date: [August 30, 2018, 4:29pm UTC](https://discourse.processing.org/t/error-when-searching-through-a-table/3118/3 "2018-08-30T16:29:24Z")

</div>

Thank you! That makes more sense.

With that new format however, what do I put in the getStringColumn() method? I previously had getStringColumn(“Name”)

---

<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: [August 30, 2018, 6:32pm UTC](https://discourse.processing.org/t/error-when-searching-through-a-table/3118/4 "2018-08-30T18:32:26Z")

</div>

> [@kat](#):
>
> If any of the words that the user typed in exists in the Table, I want the category to be printed.

- Those category words are the title headers of the Table.
- We’re gonna need 1 loop for each word entry.
- A second 1 to iterate over each TableRow of the Table.
- And a third 1 to iterate over each column of the current TableRow.
- Thus we need a triple loop in order to find out all categories which the word entries belong to.
- In my solution, I’ve defined 3 functions: **findTitles()**, **findElemTitle()**, **titleWord()**.
- **titleWord()** merely capitalizes the 1st `char` of its String parameter.
- **findTitles()** got the outer loop, which calls **findElemTitle()**, passing the current word, and collecting its returned String in a StringList container via the StringList::**append()** method.
- **findElemTitle()** receives the passed word, and has a double loop for the TableRow & its columns.
- Inside its double loop, it calls TableRow::**getString()** and compare it to the passed word via String::**equals()**.
- If they both match, **findElemTitle()** prematurely returns its corresponding TableRow::**getColumnTitle()**.
- If nothing matches after its double loop finishes, an empty String is returned instead.

```auto
/**
 * Find Title of Each Word (v1.0.2)
 * GoToLoop (2018/Aug/30)
 * Discourse.Processing.org/t/error-when-searching-through-a-table/3118/4
 */

static final String FILENAME = "Animals.csv";
static final String ANIMALS = " , whale,liZard, ant, Bee , ";
static final char DELIM = ',';

Table t;

void setup() {
  t = loadTable(FILENAME, "header");

  println(ANIMALS, ENTER);
  final String[] categories = findTitles(t, ANIMALS, DELIM);
  printArray(categories);

  exit();
}

static final String[] findTitles(final Table t, final String s, final char d) {
  if (t == null | s == null || s.isEmpty()) return new String[] {};

  final StringList sl = new StringList();

  for (final String w : trim(split(s, d))) {
    final String title = findElemTitle(t, titleWord(w));
    if (!title.isEmpty()) sl.append(title);
  }

  return sl.array();
}

static final String findElemTitle(final Table t, final String s) {
  if (t == null | s == null || s.isEmpty()) return "";

  final int cols = t.getColumnCount();

  for (final TableRow tr : t.rows()) for (int i = 0; i < cols; ++i)
    if (s.equals(tr.getString(i))) return t.getColumnTitle(i);

  return "";
}

static final String titleWord(final String s) {
  if (s == null || s.isEmpty()) return "";

  final StringBuilder sb = new StringBuilder(s.toLowerCase());
  sb.setCharAt(0, Character.toUpperCase(s.charAt(0)));

  return sb.toString();
}

```

---

<div class="post-metadata">

### Author: ![kat](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kat](https://discourse.processing.org/u/kat)
#### Post date: [August 31, 2018, 3:41pm UTC](https://discourse.processing.org/t/error-when-searching-through-a-table/3118/5 "2018-08-31T15:41:55Z")

</div>

Yay it worked!!! Thank you so so much!! I never would’ve figured this out on my own.
