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.

18<br />

Data Input<br />

“ A million monkeys were given a million typewriters. It’s called the Internet. ”<br />

—Simon Munnery<br />

In this chapter:<br />

– Manipulating Strings.<br />

– Reading and writing text fi les.<br />

– HTTP requests, parsing HTML.<br />

– XML, RSS feeds.<br />

– The Yahoo search API.<br />

– The applet sandbox.<br />

Data Input 325<br />

Th is chapter will move beyond displaying text and examine how <strong>to</strong> use String objects as the basis for<br />

reading and writing data. We will start by learning more sophisticated methods for manipulating Strings ,<br />

searching in them, chopping them up, and joining them <strong>to</strong>gether. Afterward, we will see how these skills<br />

allow us <strong>to</strong> use input from data sources, such as text fi les, web pages, XML feeds, and third party APIs,<br />

and take a step in<strong>to</strong> the world of data visualization.<br />

18.1<br />

Manipulating Strings<br />

In Chapter 17, we <strong>to</strong>uched on a few of the basic functions available in the Java String class, such as<br />

charAt( ) , <strong>to</strong>UpperCase( ) , equals( ) , and length( ) . Th ese functions are documented on the <strong>Processing</strong><br />

reference page for Strings. Nevertheless, in order <strong>to</strong> perform some more advanced data parsing<br />

techniques, we will need <strong>to</strong> explore some additional String manipulation functions documented in the<br />

Java API (more about the Java API <strong>to</strong> come in Chapter 23).<br />

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html<br />

Let’s take a closer look at the following two String functions: indexOf( ) and substring( ) .<br />

indexOf( ) locates a sequence of characters within a String . It takes one argument, a search String , and<br />

returns a numeric value that corresponds <strong>to</strong> the fi rst occurrence of the search String inside of the String<br />

being searched.<br />

String search = " def";<br />

String <strong>to</strong>BeSearched = " abcdefghi";<br />

int index = <strong>to</strong>BeSearched.indexOf(search);<br />

The value of index<br />

in this example is 3.<br />

Strings are just like arrays, in that the fi rst character is index number zero and the last character is the<br />

length of the String minus one.

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

Saved successfully!

Ooh no, something went wrong!