YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Update the String Variable

Get Embed Code
13 Languages

Showing Revision 3 created 05/24/2016 by Udacity Robot.

  1. Just like you can change the value
    of an integer variable after you've
  2. created it, you can also change
    the value of a string variable.
  3. You can update it to
    a completely new value.
  4. In our Just Java app, say we wanted to
    display a special drink of the day.
  5. Then we could use a string variable
    to store the name of that drink.
  6. Here we declare a string
    variable called drinkOfTheDay and
  7. we initially set it to the value Latte.
  8. With our box analogy this
    is what it would look like.
  9. We have the Latte value
    stored inside this variable.
  10. On another line of code we can say
    drinkOfTheDay equals a new string value.
  11. Now the word Espresso is
    stored in this string.
  12. We don't need to specify
    the data type string again,
  13. because it was already
    declared as a string up here.
  14. On the next line of code if we say
    drinkOfTheDay equals Green Tea,
  15. then we store Green Tea
    into this variable now.
  16. Here's another example where we might
    want to display the store hours
  17. within our app.
  18. We can create a string variable
    to store the text that
  19. should be displayed on the screen.
  20. The initial value is Open
    today from 8AM to 5PM.
  21. On a different day we can update the
    string variable to say Open today from
  22. 8AM to 2PM, because we want to
    leave early to go to the beach.
  23. Or on another day we can update
    the store hours to say Closed today,
  24. because we don't work on Sundays.
  25. If our app had a running list of
    all the drinks that the user wanted
  26. then we could create a string
    variable called drinksOrdered.
  27. We could start it off with one drink,
    like 1 mocha.
  28. In the variable we would
    store the text 1 mocha.
  29. And then if the user
    wants another drink,
  30. then we can just concatenate the new
    drink onto the existing drinksOrdered.
  31. This gets stored inside the variable
    drinksOrdered like this.
  32. If I want to add another drink,
  33. I can just concatenate that onto
    the existing string drinksOrdered.
  34. This expression evaluates to one long
    string that says 1 mocha, 1 cappuccino,
  35. 1 macchiato.
  36. All of that gets stored into
    this variable like this.
  37. It might look a little
    funny to have a comma and
  38. a space at the beginning of this string,
  39. but that just makes the list look nice
    with commas in between each item.
  40. The reason why I didn't put a comma
    at the end of cappuccino is because
  41. I can't be sure if there's going to be
    another drink coming after that or not.
  42. If there's only two drinks,
    then I will say 1 mocha,
  43. 1 cappuccino and that looks fine as is.
  44. If there's another drink coming,
    like a macchiato,
  45. then I can just add a comma because
    I know another drink is coming.
  46. Go ahead and
    try this out in Android Studio.
  47. Add another line of code that updates
    the string variable to a new value,
  48. or you can update it to the current
    value plus some more stuff.
  49. Once you're comfortable with knowing
    how to update string variables in
  50. Android Studio, then go ahead and
  51. implement this final behavior
    in the app for lesson two.
  52. When you change the quantity and hit the
    Order button, it should say Total and
  53. the price, and then Thank you!
  54. Technically you don't need to update the
    string variable in order to build out
  55. this functionality.
  56. However, I'd still like you to do so
    if you can.
  57. In the next lesson you're going to need
    to update string variables more often,
  58. specifically because there's going to
    be more fields in this order form and
  59. we're going to need to
    build up the order summary.
  60. When you're done,
    check these boxes to continue.
  61. This is the last coding task
    of the lesson, so good luck.