06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Data Input 329<br />

Th e reverse of split( ) is join( ). join( ) takes an array of Strings and joins them <strong>to</strong>gether in<strong>to</strong> one long String.<br />

Th e join( ) function also takes two arguments, the array <strong>to</strong> be joined and a separa<strong>to</strong>r . Th e separa<strong>to</strong>r can<br />

either be a single character or a String of characters.<br />

Consider the following array:<br />

String[] lines = { " It", "was", "a", "dark", " and" , "s<strong>to</strong>rmy", "night." } ;<br />

Using the � opera<strong>to</strong>r along with a for loop, we can join a String <strong>to</strong>gether as follows:<br />

// Manual Concatenation<br />

String onelongstring = " " ;<br />

for (int i = 0; i < lines.length; i + + ) {<br />

onelongstring = onelongstring + lines[i] + " ";<br />

}<br />

Th e join( ) function, however, allows us <strong>to</strong> bypass this process, achieving the same result in only one line<br />

of code.<br />

18.3<br />

// Using <strong>Processing</strong>’s join()<br />

String onelongstring = join(lines, " ");<br />

Exercise 18-5: Split the following String in<strong>to</strong> an array of fl oating point numbers and<br />

calculate the average.<br />

String floats = " 5023.23:52.3:10.4:5.9, 901.3---2.3 ";<br />

float[] numbers = ________(________(________, "________"));<br />

float <strong>to</strong>tal = 0;<br />

for (int i = 0; i < numbers.length; i + + ) {<br />

}<br />

________ += ________;<br />

float avg = ____________________;<br />

Reading and Writing Text Files<br />

Data can come from many diff erent places: web sites, news feeds, databases, and so on. When developing<br />

an application that involves a data source, such as a data visualization, it is important <strong>to</strong> separate out the<br />

logic for what the program will ultimately do with the data from the retrieval of the data itself.<br />

In fact, while working out the visuals, it is especially useful <strong>to</strong> develop with “ dummy ” or “ fake ” data. In<br />

keeping with our one-step-at-a-time mantra, once the meat of the program is completed with dummy<br />

data, you can then focus solely on how <strong>to</strong> retrieve the actual data from the real source.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!