Problem overview
A shop search page shows the top-rated brand for a product. In this first version there is only one product and one brand, stored in three values: product, brand and rating. The program prints two lines: the product name, then a ranked entry written as 1. Brand (rating).
For Laptop, TechPro and a rating of 4 the output is:
Laptop
1. TechPro (4)
The lesson asks you to keep each line in its own variable, productLine and brandLine, and to print them with a single println.
Approach: build each line, then join them
The first line is just the product name. The second is made of five pieces, in the order you read them: the rank "1. ", the brand, " (", the rating and ")". Scala joins strings with +, and when one side is a String and the other an Int, it converts the number to text itself. So rating goes straight into the chain.
The two lines are then joined with "\n", the newline character, which starts a new line wherever it appears in a string. One println prints the whole text, and the line break between the two parts comes from the \n you put there.
Step by step
- The product line. Create
val productLine = product. It looks redundant, but it gives every output line a variable of its own, so the last step only has to join them. - The brand line. Create
brandLineby chaining the pieces with+:"1. ",brand," (",rating,")". Mind the space before the opening parenthesis and the absence of a space after it. - Print. Join
productLine,"\n"andbrandLine, and pass the result toprintln.
The solution in Scala
val product = "Laptop"
val brand = "TechPro"
val rating = 4
val productLine = product
val brandLine = "1. " + brand + " (" + rating + ")"
println(productLine + "\n" + brandLine)
Details that matter in Scala:
valis enough. None of these values change after they are set, so there is no need forvar.- Types are inferred.
ratingis anIntand the others areStrings, without any annotation. - Left to right.
+is evaluated from left to right. Because"1. " + brandis already aString, each later+joins a string with the next piece. With two numbers at the start the order would change the result:1 + 1 + "x"gives2x, while"x" + 1 + 1givesx11. - Interpolation is an alternative.
s"1. $brand ($rating)"builds the same string as the+chain; comparing the two with==givestrue. The lesson uses+, and both are fine.
Edge cases and common mistakes
- Spacing. The output has to match exactly, so
1.TechPro(4)or1. TechPro ( 4 )will not match1. TechPro (4). - Forgetting
\n. Without it both parts end up on one line:Laptop1. TechPro (4). - Printing twice. Two
printlncalls would print the same two lines, but the lesson asks for one joined string so that the line variables are what is printed. - A space in the wrong place. The space belongs inside the quotes, in
" (", because+adds nothing between its operands.
Key takeaways
+joins aStringwith another value, converting numbers to text."\n"inside a string starts a new line.- Keeping each output line in a named
valmakes it easy to change one line later.
