Start learning free

LESSON 67 OF 291 · STRINGS I

Top K Brands V1 in Kotlin: Building Output Lines

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(...).

Kotlin'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

    KOTLIN · STRINGS I

    Top K Brands V1 in Kotlin, explained

    Problem overview

    This is the first version of a small project: a search page that shows the top-rated brand for a product. In version 1 there is one product and one brand, so nothing needs to be compared yet. The task is to build the two output lines and print them.

    The starter code gives product = "Laptop", brand = "TechPro" and rating = 4. The program must print:

    Laptop
    1. TechPro (4)
    

    The first line is the product name. The second is a ranked entry in the form 1. Brand (rating): the rank 1., the brand, and the rating in parentheses.

    Approach

    Each output line gets its own variable, productLine and brandLine. Then one println joins the two with a newline character between them. Building lines as separate values first is a habit worth forming: later versions will need to produce many such lines, and keeping each one in a variable makes the code easy to read and change.

    The ranked entry mixes text and a number, so it is built by joining pieces: "1. ", the brand, " (", the rating and ")".

    Step by step

    1. Create productLine as a val equal to product. The first line is just the product name, so nothing needs to be added.
    2. Create brandLine. Join, in the order you read them, the rank text "1. ", brand, the text " (", rating and the text ")". The number turns into text by itself because the left side of each + is a String.
    3. Print both lines. Join productLine, "\n" and brandLine, and pass the result to println. The escape \n inside a string is one newline character, so the text appears on two lines.

    The solution in Kotlin

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

    Kotlin details worth knowing:

    • String + Int. The + operator on a String converts whatever is on its right to text, so "... (" + rating works with the Int rating. The reverse, rating + " (", doesn't compile, because Int has no plus that takes a String. In this solution every + has a string on its left, because the expression starts with "1. ".
    • Escapes. "\n" is a newline. A println of a string that contains \n prints the line break and then ends its own line, so the program's output ends after 1. TechPro (4).
    • The same text with a string template. Kotlin can insert values into a string with $name or ${expression}: "1. $brand ($rating)" builds the same text as the lesson's concatenation. Templates are often shorter; this lesson practises + because it is the basic tool and works with any expression.
    • val. Every variable here is assigned once and never changed, so each one is a val.
    • Types are inferred. productLine and brandLine are Strings without being declared as such.

    Edge cases and common mistakes

    • Missing spaces. The output must be exactly 1. TechPro (4): a space after the period, a space before the parenthesis and none after it. A missing or extra space makes the lesson's tests fail.
    • The rank text. The 1. is part of the text and is the same for every brand in this version. It is not a calculated value yet.
    • Different data. The code works for any product, brand and rating. With product = "Phone", brand = "NeoByte" and rating = 5, it prints Phone and then 1. NeoByte (5).
    • Two println calls. They would print the same two lines, but the lesson asks for the lines to be joined with "\n" and printed with one call.
    • Forgetting the parentheses. They are text, so they need quotes: " (" and ")".
    • Starting the join with a number. If an expression started with rating + ... followed by a string, the compiler would reject it. Start with a string.

    Testing

    product, brand, ratingoutput
    "Laptop", "TechPro", 4Laptop, then 1. TechPro (4)
    "Phone", "NeoByte", 5Phone, then 1. NeoByte (5)
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free