Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Creating strings

From: Interactive Data Visualization with Processing

Video: Creating strings

In the movies up to this point we have been dealing with what are called primitive variables, that is variables that hold just one value at a time or arrays of primitive variables. On the other hand, another very common kind of variable, if you want to consider it that, is a string. And a string is not a variable because it actually consists of a number of character variables. So strings are usually used for analyzing texts, and in this movie, I want to show you how to create strings and in next one, I'll show you how to work with them.

Creating strings

In the movies up to this point we have been dealing with what are called primitive variables, that is variables that hold just one value at a time or arrays of primitive variables. On the other hand, another very common kind of variable, if you want to consider it that, is a string. And a string is not a variable because it actually consists of a number of character variables. So strings are usually used for analyzing texts, and in this movie, I want to show you how to create strings and in next one, I'll show you how to work with them.

I'm going to start by putting a comment with my file name here at the top, and I'm going to start by manually entering some text. This is the easy way to do it. What I do is, first as I need to declare the variable type. Now this one is String, please note it's a capital S. This is important, Processing is case sensitive, and also it's because this one is an object, it's not an individual variable and that's why it's got this capitalization.

I need to give a name to this one, I'm actually going to use a little line from the movie the Deer Hunter, and type equals and then in quotes, I'll put, "This is this. It's not something else." If I recall correctly, and then I finish with quotes and then a semicolon (;). So now I actually have a string of text that I can use, and what I can do is if I want to double-check on it, I can do a print line, and that's a long variable name, so I'm going to copy it and I can run it, and then you see that I have the text showing up down there in the bottom of the console.

On the other hand, a lot of times you actually want the text to show up somewhere else. And so what I'm going to do is I'm going to create a window here, 600x200 that we've been using so far. Turn on anti-aliasing. I'm going to give it a light gray background color, I'm going to give a fill, if you want the text to appear in a particular color, you need to give it a fill. This one, I'm going to go with the green, and then I'm going to place the text, use the function text, then give the name of the variable or the string.

It's telling me to put it right there and then where you're going to position it. In this one, I am going to put 100 pixels over and 140 pixels down, and I press Run and what I get is some bright green text on the light gray background. By the way, the pixels determine the baseline of the text. So it has more to do with the bottom left of the first line of text. That's a little hard to read, I'm actually going to change it to just straight black. I can do that by just removing the fill.

If I just come in back here, I am changing these two 0, 0s all the way through. There we go. That's the simple version. I do want to show you another thing that I'm going to do a little bit differently. And that's about reading text from an external file because that's usually how you're going to get your text. And this is going to be the first movie where we actually have a file to start with. If you go up to the folder for this movie, you'll see there is a file there called Szymborska, it's a text document.

If you double-click on it, you'll see it's a poem by the poet Laureate of Poland, Wislawa Szymborska called A Contribution to Statistics. It's a wonderful piece. And what you can do to add this to Processing, the easy way is to move over your whole folder window here and just drag the file onto the Processing window. And you can see right here, it says, one file added to the sketch on the bottom, and back in the window over here, you see that we now have a new folder called data, and we have this Szymborska text within that one.

Now normally, you would be dragging the text in from another place and of the same folder as the sketch already, but Processing creates a data folder and that's where it puts text, that's where it put images or fonts or other things that it needs to deal with. It just automatically, if you just drag the file into the Processing window, so I can close that one now. And what I want to show you is how to access that text and to read it a little bit. What I'm going to do is I'm going to create another string variable. And this one, I'll call it poemLines.

When you have a text file, each line of text is its own string, and so actually we're creating an array here because we have many lines of text. So I'm calling it poemLines, it's an array. And then the function that we want to use is loadStrings, and then in parentheses, and in quotation marks, we put the name of the text ("Szyborska.txt"), well, I'm missing a letter.

And then what I can do is I can do print line, and if I do poemLines, now what it's going to do is it is going to print the entire contents of the array which is the entire text file, so you don't want to do this if you have a huge thing. But this one isn't real long, so I can just hit Run, I get my Deer Hunter quote here, and then down here you can see that we have our array with every line of the poem.

It does a couple of funny things. Number one is, it's putting quotes around every line which is a little distracting. The other one is that there are blank lines in the poem and it's putting quotes with a blank there also, which isn't exactly what we want, but what we can tell is that it has successfully read the file. And I want to just a little something else here. I want to show you how you can read just one line at a time if you want. So right here, I have poemLines and that's going to read the whole entire thing. Comment that one out.

Let's read just a few lines, so for instance let's read zero and one. I can put index numbers in square brackets. If you want to refer to just one item in an array, that's how you do it. And I'm going to copy that, I'm going to put it on a separate line, and what this is telling is that I want to print the very first line which is index number zero and then the next line index number one in the poem. And now when I run it, there you can see I've got my first two lines. And so this is the initial way of creating arrays.

You can either enter them manually, like I did with my example of this quote, or you can load an external text file which is the most common way of doing this and then dealing with an array of strings that result from it.

Show transcript

This video is part of

Image for Interactive Data Visualization with Processing
Interactive Data Visualization with Processing

72 video lessons · 12242 viewers

Barton Poulson
Author

 
Expand all | Collapse all
  1. 3m 16s
    1. Welcome
      58s
    2. What you should know
      1m 22s
    3. Using the exercise files
      56s
  2. 11m 51s
    1. Overview of data visualization
      11m 51s
  3. 11m 53s
    1. Installing Processing
      3m 38s
    2. Overview of Processing
      4m 5s
    3. Exploring libraries
      4m 10s
  4. 1h 1m
    1. Basic setup
      7m 31s
    2. Drawing points
      4m 37s
    3. Drawing lines
      5m 6s
    4. Drawing ellipses and circles
      5m 24s
    5. Drawing arcs
      6m 54s
    6. Drawing rectangles and squares
      4m 58s
    7. Drawing quadrangles
      3m 25s
    8. Drawing triangles
      2m 55s
    9. Drawing polygons
      3m 37s
    10. Drawing simple curves
      4m 54s
    11. Drawing complex curves
      6m 46s
    12. Drawing Bézier curves
      5m 38s
  5. 54m 3s
    1. Introduction to variables
      10m 44s
    2. Understanding variable scope
      6m 53s
    3. Modifying variables
      9m 8s
    4. Creating arrays
      9m 53s
    5. Modifying arrays
      6m 37s
    6. Creating strings
      7m 3s
    7. Modifying strings
      3m 45s
  6. 1h 2m
    1. Incorporating randomness
      7m 59s
    2. Using Perlin noise
      4m 24s
    3. Shuffling with Java
      3m 30s
    4. Specifying line attributes
      8m 2s
    5. Changing placement modes
      5m 45s
    6. Understanding color attributes and functions
      4m 16s
    7. Exploring color spaces
      7m 44s
    8. Using color palettes
      7m 5s
    9. Transforming the grid
      8m 38s
    10. Exploring the attribute matrix
      5m 33s
  7. 52m 7s
    1. Building code blocks
      5m 57s
    2. Writing a while loop
      3m 52s
    3. Using for loops
      5m 35s
    4. Creating conditionals
      14m 50s
    5. Working with easing
      10m 51s
    6. Creating spirals
      11m 2s
  8. 18m 55s
    1. Mouse tracking
      3m 54s
    2. Hovering and clicking
      11m 16s
    3. Understanding keyboard interaction
      3m 45s
  9. 27m 32s
    1. Specifying fonts
      6m 43s
    2. Using images
      5m 51s
    3. Playing a video loop
      6m 20s
    4. Exporting video
      3m 47s
    5. Adding sound
      4m 51s
  10. 20m 49s
    1. Creating functions
      11m 48s
    2. Creating classes and objects
      9m 1s
  11. 31m 10s
    1. Using embedded data
      5m 26s
    2. Working with appended text data
      6m 4s
    3. Working with appended tabular data
      10m 26s
    4. Reading XML data
      9m 14s
  12. 48m 17s
    1. Generating dot plots
      11m 11s
    2. Building scatter plots
      10m 0s
    3. Making line plots
      9m 55s
    4. Creating bar charts
      9m 12s
    5. Checking out examples of maps, hierarchies, and networks
      7m 59s
  13. 20m 57s
    1. Introducing some principles of 2D design
      13m 44s
    2. Understanding color theory
      7m 13s
  14. 24m 46s
    1. Interacting with zooming, rotating, and sliding
      6m 26s
    2. Implementing slicing
      6m 47s
    3. Using rollovers
      5m 58s
    4. Introducing the GUI libraries
      5m 35s
  15. 10m 35s
    1. Sharing via OpenProcessing and other sites
      3m 19s
    2. Saving as a desktop application
      2m 42s
    3. Saving as JavaScript
      1m 47s
    4. Saving as an Android application
      2m 47s
  16. 2m 38s
    1. Where to go from here
      2m 38s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Interactive Data Visualization with Processing.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.