# Java Processing find matching text in a table and get the value from next to it

**URL:** <https://discourse.processing.org/t/java-processing-find-matching-text-in-a-table-and-get-the-value-from-next-to-it/36268>\
**Category:** Coding Questions\
**Created:** [April 11, 2022, 2:52am UTC](https://discourse.processing.org/t/java-processing-find-matching-text-in-a-table-and-get-the-value-from-next-to-it/36268 "2022-04-11T02:52:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oZeppy](https://avatars.discourse-cdn.com/v4/letter/o/b5e925/32.png) [@oZeppy](https://discourse.processing.org/u/oZeppy)\
**Post date:** [April 11, 2022, 2:52am UTC](https://discourse.processing.org/t/java-processing-find-matching-text-in-a-table-and-get-the-value-from-next-to-it/36268/1 "2022-04-11T02:52:46Z")

</div>

I have a table and am trying to find a specific string within it, once found I’m trying to get a string from next to it. How would I go about doing that?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 11, 2022, 2:43pm UTC](https://discourse.processing.org/t/java-processing-find-matching-text-in-a-table-and-get-the-value-from-next-to-it/36268/2 "2022-04-11T14:43:33Z")

</div>

> **[Reference](https://processing.org/reference/Table_findRow_.html)**
>
> Finds the first row in the Table that contains the value provided, and returns a reference to that row. Even if multiple rows are possible matches, only the first matching row is returned. The …

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 11, 2022, 2:44pm UTC](https://discourse.processing.org/t/java-processing-find-matching-text-in-a-table-and-get-the-value-from-next-to-it/36268/3 "2022-04-11T14:44:56Z")

</div>

int findRowIndex(java.lang.String value,  
int column)

See [Table](http://processing.github.io/processing-javadocs/core/processing/data/Table.html#findRowIndex-java.lang.String-int-)

**Example**

```auto

// Sketch saves the following to a file called "new.csv":
// id,species,name
// 0,Panthera leo,Lion etc. 

void setup() {

  size(200, 60);

  // make table 
  Table table = makeTable();
  // show table 
  showTable(table); 
  // save table 
  saveTable(table, "data/new.csv");

  // search table
  println("-----------");
  for (int i_Column=0; i_Column<table.getColumnCount(); i_Column++) {
    int resultRowIndex = table.findRowIndex("Tiger2", i_Column);
    if (resultRowIndex>-1) {
      println(resultRowIndex);
      String result1=table.getString( resultRowIndex, i_Column );
      println(result1);
      String resultNext=table.getString( resultRowIndex, i_Column+1 );
      println(resultNext);
    }//if
  }//for

  // test search  
  println("--------");
  int result = table.findRowIndex("Panthera leo", "species");
  println(result);
}//func 

//--------------------------------------------------------------------------------------

Table makeTable() {

  Table table = new Table();

  table.addColumn("id");
  table.addColumn("species");
  table.addColumn("name");

  // fill table with data
  TableRow newRow = table.addRow();
  newRow.setInt("id", table.lastRowIndex());
  newRow.setString("species", "Panthera leo");
  newRow.setString("name", "Lion");

  newRow = table.addRow();
  newRow.setInt("id", table.lastRowIndex());
  newRow.setString("species", "Tiger");
  newRow.setString("name", "White Tiger");

  for (int i_dummy=0; i_dummy<8; i_dummy++) {
    newRow = table.addRow();
    newRow.setInt("id", table.lastRowIndex());
    newRow.setString("species", "Tiger"+i_dummy);
    newRow.setString("name", "White Tiger"+i_dummy);
  }

  return table;
}

void showTable(Table table) {

  // print header 
  println("-------------------------------------------------------");
  TableRow row = table.getRow(0);
  for (int i_Column=0; i_Column<table.getColumnCount(); i_Column++) {
    print("|");
    print(row.getColumnTitle(i_Column));
    printSpaces(row.getColumnTitle(i_Column));
  }
  print("|");
  println();
  println("-------------------------------------------------------");

  // print content 
  for (int i_Row=0; i_Row<table.getRowCount(); i_Row++) {
    for (int i_Column=0; i_Column<table.getColumnCount(); i_Column++) {
      print("|");
      String content=table.getString( i_Row, i_Column );
      print(content);
      printSpaces(content);
    }
    print("|");
    println();
  }
  println("-------------------------------------------------------");
  println();
}

void printSpaces(String prev) {
  // Tool to print a table.
  // Prints a String of Spaces to be inserted after "prev" to fill the space up to a position 17 which is the end of the column ("|")
  String stringSpaces=""; 
  for (int i=0; i< 17-prev.length(); i++) {
    stringSpaces+=" ";
  }
  print(stringSpaces);
}
//

```
