Storing String Data in Arrays?

Hi there–so I am trying to write something that can input text and then store what is there to an array. Most of the array tutorials I have seen are about numeric data. Is it any different for storing text? Furthermore, is there a way to print out the array’s contents to verify that the data is actually in there, piece by piece? The documentation on this is very confusing.

I have the variables for this down but it’s where to go from there that gives me issues. I can do it for an individual string but the arrays confuse me.

1 Like

the basic would be a string array storing lines:

String[] lines = new String[5];
String line="";
int l = 0;

void setup() {}
void draw() {}
void keyPressed(){
  if ( keyCode == ENTER ) {
    lines[l]=line;
    line = "";
    l++;
  } 
  else line += key;
// 
  println("line "+line);
  println(lines); //printArray(lines);
}

the array of lines fits to
https://processing.org/reference/loadStrings_.html
https://processing.org/reference/saveStrings_.html
file handling.

as adding and deleting of array / lines is difficult also check on
https://processing.org/reference/StringList.html

1 Like

I couldn’t resist…

A mini editor…

String helpText = 
  "My little editor \n\n"
  +"Type text.\n"
  +"Hit enter to go to the next line.\n"
  +"Use Backspace.\n"
  +"Use Cursor up and down.\n\n"
  +"Esc is disabled; use Alt-F4 instead.";

String[] lines = new String[55];
int lineCounter = 0; // current 
int prevHighestLineCounter = 0; // this is to know how far crs down is allowed to go down  

// cursor sign 
boolean cursorBlinkFlag; 
int timerCursorBlink;

// ------------------------------------------
// Processing Core Functions  

void setup() {
  size(660, 660);
  background(255);

  lines[lineCounter]="";
}//setup()

void draw() {
  background(255);
  showHelpText(); 
  showTextArray();
}

// ------------------------------------------
// Inputs 

void keyPressed() {
  // eval key 
  if ( keyCode == ENTER || keyCode == RETURN ) {
    // new line 
    lineCounter++; // pitfall
    lines[lineCounter]="";
    prevHighestLineCounter = lineCounter;
  } else if (keyCode==BACKSPACE) {
    lines[lineCounter] = shortenStringByOneSign(lines[lineCounter]);
  } else if (key==ESC) {
    key=0; // kill ESC
  } 
  // CURSOR KEYS
  else if (keyCode==UP) {
    if (lineCounter>0) {
      lineCounter--;
    }//if
  } else if (keyCode==DOWN) {
    if (lineCounter<prevHighestLineCounter) {
      lineCounter++;
    }//if
  } else if (keyCode==LEFT) {
    // ignore
  } else if (keyCode==RIGHT) {
    // ignore
  }
  // IGNORE 
  else if (keyCode==ALT) {
    // ignore
  } else if (keyCode==CONTROL) {
    // ignore
  } else if (keyCode==SHIFT) {
    // ignore
  } 
  // LETTERS 
  else if (keyCode>30) {
    // normal text input 
    lines[lineCounter] += key;
  } 
  // IGNORE 
  else {
    // ignore
  } //else 
  // 
  // println("line "+line);
  // println(lines); //printArray(lines);
}

// ------------------------------------------
// Minor Tools 

void showHelpText() {
  // Yellow box
  fill(#F6FA1C); // Yellow
  stroke(0);     // Black
  rect(width-225, 3, 
    210, 122);

  // Black text
  fill(0); // Black
  text(helpText, 
    width-220, 20);
}

void showTextArray() {
  int i=0; 

  // for 
  for (String oneLine : lines) {
    if (oneLine!=null) {
      // Is it the current/active line? 
      if (i==lineCounter) {
        // show text cursor
        text(oneLine + cursorBlink(), 
          9, i*22+20);
      } else {
        // no cursor
        text(oneLine, 
          9, i*22+20);
      }//else
      i++;
    }//if
  }//for
}//func 

String shortenStringByOneSign( String strIn_) {
  if (strIn_.length()>0)
    return 
      strIn_.substring( 0, strIn_.length()-1 );
  else 
  return "";
}

String cursorBlink() {
  // manages the blinking of the cursor 
  if (millis()-timerCursorBlink>330) {
    cursorBlinkFlag = !cursorBlinkFlag; // toggle
    timerCursorBlink=millis();
  }//if

  if (cursorBlinkFlag) 
    return "|" ;
  else return"";
}
3 Likes

These have definitely been helpful! Thanks!!

Now for the next question–let’s say we need to store a name and address of someone. How can we store these under one person? For example, let’s say we have Bob and we need to associate the address we input, let’s just call it 1234 5th Street.

I have the basic basics down, now it’s just time for the next step with specific storage.

what is closer to a database ( compared to a array of string lines )
would be
https://processing.org/reference/Table.html
https://processing.org/reference/JSONObject.html

The table may be closer to what I want here…now, how do I have user input for a table? Everything else with setup is pretty self-explanatory.

in my example above the keyboard input is aggregated in “line”
and at ENTER stored to array entry lines[l],
you should store it to a table cell

1 Like

Depending on what you want to do

You could have 3 different screens:

  • show entire table as a list

  • Show one person only on a screen with multiple lines

  • Show a form for one person where you can fill out his or her data. On sending the input text fields are copied into your table. That’s not totally easy since you have multiple fields for text but only one is selected. Only this changes when we type. Use mouse or tab to get to another text field.

You would only have one table that serves all 3 screens.

You can load and save this table

from workflow this should come first,
make your “data/mydata.csv” file with header first
( like in spreadsheet software OR notepad )
and test the loadTable defaults ( columncount , rowcount, select row and print all …)

String filename  = "data/mydata.csv";

/*
id,name,street,town,code,
 1,you,yourstreet,yourtown,1174,
 0,me,mystreet,mytown,4711,
 */

Table mytable;
int tcols, trows, numcol = 0;
boolean dprint = true;

void get_tabledata() {                             // get tabledata from file
  mytable = loadTable(filename, "header, csv");
  tcols = mytable.getColumnCount();
  trows = mytable.getRowCount();
  if (dprint) { 
    println("total cols in table: "+tcols);
    println("total rows in table: "+trows+", header line not counted ");
    println("header: ");
    for ( int i = 0; i < tcols; i++)  println("headerline col: "+i+" "+ mytable.getColumnTitle(i));
    for ( int j = 0; j < trows; j++) {
      TableRow thisrow = mytable.getRow(j);
      for ( int i = 0; i< tcols; i++) println("row "+j+ " col "+i+" "+mytable.getColumnTitle(i)+" : "+thisrow.getString(i));
    }
  }
}

void sort_tabledata() {                             // sort numerically a specific column
  mytable.setColumnType(numcol, Table.INT); 
  mytable.sort(numcol);  
  if (dprint) {
    println("list all rows column "+numcol+" _"+mytable.getColumnTitle(numcol)+"_ sorted by number");
    for ( int i = 0; i < mytable.getRowCount(); i++ ) {
      TableRow thisrow = mytable.getRow(i);
      println("row "+i+" "+thisrow.getInt(numcol));
    }
  }
}

void save_tabledata() {                             // save to file
  saveTable(mytable, filename, "csv");
  println("and saved back to "+filename);
}

void setup() {
  get_tabledata();
  sort_tabledata();
  save_tabledata();
  exit();
}

this might not look so nice, as i not use the column names like
thisrow.getString("name")
instead i use the column number 0…colcount
only because this code fits to all CSV with header structure
(ok besides the sort column 0 setting )

1 Like

I see. Interesting.

I didn’t know that. You could do this in Excel.

I attempted always to build the tables in processing which takes much longer…

Alternatively

Alternatively we could have a class Person and an ArrayList<Person> persons of it

1 Like

just a idea for a data table

  • from CSV file
  • read to table and store to array of class
  • show spreadsheet style
  • click on cell for edit mode /
    • edit content in edit window
    • save with [enter] ( to array )
  • save with [s] to table and table to file.


revision: here

should work with any CSV file,
but currently expect first column is number / like index to sort on /