Start learning free

LESSON 67 OF 291 · STRINGS I

Top K Brands V1 in Scala: One Brand

Practice Read the full explanation

E-commerce search page: when a shopper searches a product, show the top-rated brand selling it. In v1 there's just one product and one brand.

You start with:

        val product = "Laptop"
        val brand = "TechPro"
        val rating = 4

Build:

Laptop
1. TechPro (4)

Rules:

  • The first line is the product name.
  • The second line is a ranked entry written as "1. Brand (rating)".
  • Keep each line in its own variable (productLine and brandLine), join them with a newline "\n" and print the result with println(...).

Scala's + on a String and an Int converts the number to text for you.

productLine + "\n" + brandLine joins both lines into one string.

        val product = "Laptop"
        val brand = "TechPro"
        val rating = 4

        val productLine = product
        val brandLine = "1. " + brand + " (" + rating + ")"
        println(productLine + "\n" + brandLine)

STEP 1 OF 3
    Ctrl + Enter to check

    SCALA · STRINGS I

    Top K Brands V1 in Scala, explained

    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

    1. 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.
    2. The brand line. Create brandLine by chaining the pieces with +: "1. ", brand, " (", rating, ")". Mind the space before the opening parenthesis and the absence of a space after it.
    3. Print. Join productLine, "\n" and brandLine, and pass the result to println.

    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:

    • val is enough. None of these values change after they are set, so there is no need for var.
    • Types are inferred. rating is an Int and the others are Strings, without any annotation.
    • Left to right. + is evaluated from left to right. Because "1. " + brand is already a String, each later + joins a string with the next piece. With two numbers at the start the order would change the result: 1 + 1 + "x" gives 2x, while "x" + 1 + 1 gives x11.
    • Interpolation is an alternative. s"1. $brand ($rating)" builds the same string as the + chain; comparing the two with == gives true. The lesson uses +, and both are fine.

    Edge cases and common mistakes

    • Spacing. The output has to match exactly, so 1.TechPro(4) or 1. TechPro ( 4 ) will not match 1. TechPro (4).
    • Forgetting \n. Without it both parts end up on one line: Laptop1. TechPro (4).
    • Printing twice. Two println calls 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 a String with another value, converting numbers to text.
    • "\n" inside a string starts a new line.
    • Keeping each output line in a named val makes it easy to change one line later.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free