Text and lists

This is part 2 of an N part series detailing how I make my animations.

Prev Next

You should now have:

With the following in your Keeling folder:

(You may also have some .tmp files there as well, leave them be as they are undo files for the IDE which will get cleaned up automatically.)

This post is going to detail how to get the data out of our CSV file and onto the screen.

To hit the ground running type print("hello, world"); between the curly brackets of setup() and click the play button (or press Ctrl-R). You should see

This is called the “console” and is where all the action will be taking place for this tutorial.

Click the stop button.

Now replace print("hello, world); with

String message = "hello, world";
print(message);

Clicking run will result in the same thing!

What the String? The trouble with coding is that you have to name everything, even stuff you don’t even realise needs a name. What would you call a whole bunch of characters?; a word?; a sentence?; a paragraph?; a chapter? In the distant past someone called a string of characters a String and the name stuck. A String can be of any length, including zero and simply means that we are normally referring to human readable characters.

So String message is saying please make a box called message which can only contain a string of characters.

String message = "hello, world"; is saying: in a String box called message put this string of characters: hello, world.

While print(message); is saying please print to the console whatever is contained in the string box called message.

Now replace the contents of setup() with:

String[] messages = {"hello, world", "goodbye, world"};
print(messages[1]);

Running this should display goodbye, world.

Huh!? How? Ok, there’s a lot going on here so let’s break it down:

String[] means, in an abbreviated manner, list of strings. So String[] messages is saying make me a box called messages where I can keep many strings of characters.

So, String[] messages = {"hello, world", "goodbye, world"}; is saying in a multi string box called messages add the strings hello, world and goodbye, world.

Which leads us to print(messages[1]);¹ This is saying print to the console string #1 from the box called messages. Surely this should print hello, world! Why is goodbye, world displayed?

In many computer languages lists, also known as “arrays”, are zero based. Which means to access the first item you’d use messages[0]. This trips up many a beginner and the odd seasoned coder too!

Head on to part 3 to learn how to read data from text files.


¹ Perhaps you’ve noticed the use of [1]? I hope this will clarify the meaning of String[].