Even though you’ve cached the result of the expression groceries.getString(row, 3) into the variable source:
String source = groceries.getString(row, 3);
You still keep unnecessarily repeating it on your if () {} checks:
if (groceries.getString(row, 3).equals("store")) {
This is how short the check becomes once cache variable source is used instead:
if ("store".equals(source)) {
Anyways, I’ve refactored your sketch w/ some additional tricks in order to make it clearer I hope: ![]()
/**
* Table TSV Recipe (v2.0.2)
* by David (2022/Dec/22)
* mod GoToLoop
*
* https://Discourse.Processing.org/t/
* help-with-examples-from-rapid-android-development/40257/8
*/
static final String
TSV_FILE = "groceries.tsv",
TSV_PARAMS = "header, tsv";
Table groceries;
void setup() {
size(500, 400);
noLoop();
//noStroke();
fill(0200);
rectMode(CENTER);
textSize(24);
textAlign(CENTER, CENTER);
groceries = loadTable(TSV_FILE, TSV_PARAMS);
}
void draw() {
background(0);
final int rowHeight = height / groceries.getRowCount();
for (final TableRow row : groceries.rows()) {
final String
amount = nullToEmptyStr(row.getString("amount")),
unit = nullToEmptyStr(row.getString("unit")),
item = nullToEmptyStr(row.getString("item")),
source = nullToEmptyStr(row.getString("source"));
pushStyle();
switch (source) {
case "store":
fill(255, 110, 50);
break;
case "market":
fill(50, 220, 255);
}
rect(width >> 1, rowHeight >> 1, width, rowHeight);
fill(-1);
text(amount + " " + unit + " " + item, width >> 1, rowHeight >> 1);
popStyle();
translate(0, rowHeight);
}
}
static final String nullToEmptyStr(final String str) {
return str != null? str : "";
}
amount unit item source
1 pound flour store
6 pcs eggs market
0.5 table spoon salt store
some cold water
2 pcs onions market
1 stick butter market
1/4 pound Gruyere cheese store
1/4 pound Emmental cheese store
1 bunch chives market
taste salt store
taste pepper store