Start learning free

LESSON 66 OF 285 · STRINGS I

Top K Brands V1 in Java: 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:

String product = "Laptop";
String brand = "TechPro";
int 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 System.out.println(...).

Java converts the int to text when you join it to a String with +.

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

String product = "Laptop";
String brand = "TechPro";
int rating = 4;

String productLine = product;
String brandLine = "1. " + brand + " (" + rating + ")";
System.out.println(productLine + "\n" + brandLine);

STEP 1 OF 3
    Ctrl + Enter to check

    JAVA · STRINGS I

    Top K Brands V1 in Java, explained

    Problem overview

    A shopper searches for a product, and the page shows the top-rated brand that sells it. In this first version there is just one product and one brand, so the task is formatting. The starter code has product ("Laptop"), brand ("TechPro") and rating (4, an int). The program prints two lines:

    Laptop
    1. TechPro (4)

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

    Approach: build each line, then join them

    The lesson asks for each output line to live in its own variable, productLine and brandLine, and for one println at the end. Building lines separately keeps each piece small and easy to check, and the last step only has to put them together.

    Step by step

    1. Create productLine and set it to product. The first line is just the product name.
    2. Create brandLine by joining the pieces with +, in the order you read the result: "1. ", the brand, " (", the rating, ")".
    3. Print both lines with a single System.out.println, putting "\n" between them. \n is the newline character, so the text after it starts on a new line.

    The solution in Java

    String product = "Laptop";
    String brand = "TechPro";
    int rating = 4;
    
    String productLine = product;
    String brandLine = "1. " + brand + " (" + rating + ")";
    System.out.println(productLine + "\n" + brandLine);
    

    When + joins a String and an int, Java converts the number to text. That is why rating can sit in the middle of the expression without any conversion call.

    Edge cases and common mistakes

    Where the numbers start

    + works from left to right. Once the left side is a String, every later + concatenates. But if the expression starts with numbers, they are added first. With brand = "TechPro" and r = 4, the expression 1 + r + ". " + brand gives 5. TechPro, while "" + 1 + r + ". " + brand gives 14. TechPro. The lesson's solution avoids the problem by starting the expression with the text "1. ".

    Quotes inside the string

    The parentheses are ordinary characters, so they go inside the quoted pieces: " (" before the rating and ")" after it. Only the variables brand and rating stay outside the quotes, because their values are what you want, not their names.

    Missing spaces

    The format is exact: a space after 1., a space before the opening parenthesis, and no space inside the parentheses. A missing or extra space makes the lesson's tests fail even when the words are right.

    Newline in the wrong place

    "\n" belongs between the lines. If you add it at the end as well, the output ends with an extra blank line, because println already ends the line.

    Testing

    Change the three starter values and check that only those values change in the output. With "Phone", "Zenith" and a rating of 5, the program prints:

    Phone
    1. Zenith (5)

    With "Desk", "NeoByte" and a rating of 1, it prints:

    Desk
    1. NeoByte (1)

    The 1. never changes in this version, since there is only one brand to rank. Because the rank is written directly in the string, it is plain text, not a variable.

    Key takeaways

    • Build text from pieces with +; numbers are converted automatically once a String is involved.
    • "\n" inside a string starts a new line.
    • Giving each line its own variable makes the final print trivial.
    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free