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
- Create
productLineand set it toproduct. The first line is just the product name. - Create
brandLineby joining the pieces with+, in the order you read the result:"1. ", the brand," (", the rating,")". - Print both lines with a single
System.out.println, putting"\n"between them.\nis 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 aStringis involved. "\n"inside a string starts a new line.- Giving each line its own variable makes the final print trivial.
