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

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?

1 Like

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

See Table

Example



// 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);
}
//