English subtitles

← Combining String and Integer Variables

Get Embed Code
13 Languages

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

  1. In the last video we saw how we
    can take string literals and
  2. combine them with integer literals
    using the concatenation operator.
  3. This expression evaluates to the string,
    I need two cups of coffee on Monday.
  4. The powerful thing about concatenating
    different parts of the text together
  5. is that you can swap out different
    parts if they need to change.
  6. For example, the number of
    cups of coffee might change so
  7. instead of a literal value here,
    we can change it to be a variable.
  8. Now I've removed the two and
    replaced it with a quantity variable, so
  9. when we evaluate this expression,
  10. we'll always get the latest
    value of the quantity variable.
  11. If the variable is holding the value 2,
  12. then it'll pronounce something like,
    I need 2 cups of coffee on Monday.
  13. So these parts of the string stay
    constant and they don't change,
  14. whereas the quantity is free to change
    and will always get the latest value.
  15. I'm going to try it
    out in Android Studio.
  16. Since quantity happens to be
    a variable that we already have
  17. declared in the MainActivity, I can just
    use it directly in submitOrder method.
  18. I'm going to make up a string
    like Total Item Count, and
  19. then add quantity, and then coffees.
  20. This isn't really related to price, but
  21. I just want to show you that you can
    concatenate strings with variables.
  22. Let's run this to see
    what it looks like.
  23. If I change the quantity and
  24. then hit the order button then it
    shows total item count, 3 coffees.
  25. I forgot to add the space right
    after the 3, but before coffees, so
  26. we should go and add that in.
  27. I am going to add a space here so that
    it appears right after the quantity.
  28. Notice that quantity here
    is not in double quotes,
  29. if it was then it would literally
    show quantity on the screen, but
  30. we want the value of this variable so
    we don't put it in quotes.
  31. Now that it has an extra space
    here let's try to run it and
  32. make sure it's correct.
  33. Now when I change the quantity to 3 and
    hit the Order button it says,
  34. Total Item Count 3 coffees.
  35. It correctly has a space before and
    after the quantity value.
  36. Taking a step back, when you look at
    this line of code it can be a little
  37. overwhelming because there's quotation
    marks everywhere and plus signs and
  38. spaces in weird places.
  39. Just imagine that the plus
    signs disappear and
  40. everything in the quotes
    gets squished together and
  41. that the quantity variable gets
    replaced with an actual value.
  42. Because it's not that easy to read,
    it's common to have mistakes here.
  43. You might miss an opening or
    closing quotation or
  44. put the plus in the wrong place or
    accidentally put a variable in quotes.
  45. Just read through your code carefully to
    make sure it's doing what you expect.
  46. You can also do math expressions.
  47. You can have quantity times five
    in parentheses and this will
  48. turn into a single integer value and
    that will get concatenated to Price.
  49. A string plus an integer turns into
    a string and that gets stored in
  50. the variable priceMessage and
    that gets displayed on the screen.
  51. When I run this on my device
    It shows up as Price 15.
  52. If you want the dollar sign or
    the currency symbol to show up,
  53. you need to make it part of
    the string literal, like this.
  54. The integer only holds the number part,
    not the currency symbol.
  55. If I run this on my device and
  56. then hit the Order button,
    now I see the currency symbol.
  57. Following the pattern that I've
    been showing you, go ahead and
  58. modify the submitOrder method so
  59. you can practice displaying
    different messages on the screen.
  60. First, try to display Amount Due
    $10 on the screen when you
  61. hit the Order button.
  62. Once you get that working,
    then change it so
  63. that you display this string
    when you hit the order button.
  64. Repeat the same for
    each of these different strings.
  65. You should only see one of these
    strings on the screen at one time.
  66. Remember to concatenate
    the strings with variables so
  67. that the quantity and price values are
    accurate based on the quantity picker.
  68. So, if I change this to quantity
    of five cups of coffee,
  69. then the amount due should be $25.