Start learning free

LESSON 66 OF 410 · STRINGS I

Top K Brands V1 - One Brand in Java

Practice

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

    Top K Brands V1 in Java

    Keep going with JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free