/*6.914, Example 4.1 loadStrings, split, saveStrings, join 0) loadStrings takes a text file (usually with extension .txt) and turns each line into a string, which is then put at its line index in an array. So, for instance, if you use this command: String lines[] = loadStrings("myList.txt"); lines[0] will be the first line of myList.txt, lines[1] will be the second line, and so forth. 1) You may want to store sets of information on each line, separating the pieces of information by a delimiter - for instance, a comma, a space, or a tab. You can use the "split" command to turn a line into an array where each item is separated from the next in the original line by the delimiter. The first parameter given to split is an array to split; the second parameter is the char that is used as the delimiter. This looks like: String[] newStringArray = split(String, char); So, for instance, in the file *** web.mit.edu/mish/Public/addresses.txt *** each line contains a name, a city, and a state - separated by commas. We will use split to divide up these lines. Download this file to your sketch's data folder to practice with it. 2) Saving lines is basically the reverse. Use saveStrings to save to a file: the first parameter should be the file name (will end up in your sketch's main folder.) the second parameter will be the array of strings to be saved to a file. This looks like: saveStrings(“myNewList.txt”, String[]); Just as before, you will end up with one array item per line of the file. 3) Use "join" to save an array's information into a string, given a char to use as a delimiter. String newString = join(String[], char); */ String[] myStrings, names, cities, states; int counter = 0; void setup(){ size(200, 200); PFont myFont = createFont("Tahoma", 16); textFont(myFont); // initializing arrays to be the correct size myStrings = loadStrings("addresses.txt"); names = new String[myStrings.length]; cities = new String[myStrings.length]; states = new String[myStrings.length]; // using split to divide the lines into component parts for (int i = 0; i < myStrings.length; i++){ String[] temp = split(myStrings[i], ','); names[i] = temp[0]; cities[i] = temp[1]; states[i] = temp[2]; } } // every time that draw runs, it checks the value of counter void draw(){ background(0); text(names[counter], 5, 20); text(cities[counter], 5, 40); text(states[counter], 5, 60); } // using switch/case to move between screen states void keyPressed(){ if (key == CODED){ switch(keyCode){ case LEFT: if (counter > 0) counter--; break; case RIGHT: if (counter < myStrings.length) counter++; break; } } else { switch (key){ // save to file case (TAB): // file of names, one per line saveStrings("namesFile.txt", names); // recreating the original file, using join String[] allStrings = new String[3]; for (int i = 0; i < 3; i++){ // array of strings for each person String[] temp = new String[3]; temp[0] = names[i]; temp[1] = cities[i]; temp[2] = states[i]; allStrings[i] = join(temp, ','); } saveStrings("recreatedFile.txt", allStrings); } } }