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