
Day 0 of HackerRank's 30 Days of Code looks almost too easy: print a fixed greeting, then print whatever line you were given. But this tiny task is really checking one thing, which is whether you can take input from the judge and send output back in exactly the shape it expects. Every later problem in the series depends on that skill, so it is worth getting it right on day one instead of fighting with it on day ten.
If you are in your first year and have only ever written programs that print fixed text, this is your first program that reacts to data it did not know in advance. That is the real jump here. Once you understand where the input comes from and where the output goes, the rest of the series becomes about logic, not about plumbing.
Problem
You get exactly one line of text. Store it in a variable. Then print two lines: the first is always the exact text Hello, World. (capital H, comma, capital W, full stop at the end), and the second is the line you just read, unchanged.
Input format: one line of text. It can contain spaces and punctuation.
Output format: two lines. Line 1 is Hello, World. and line 2 is the input line as it was given.
Constraints: none beyond the input being a single line.
Input:
Welcome to 30 Days of Code!
Output:
Hello, World.
Welcome to 30 Days of Code!
Approach
Think of the judge as a person who types something into your program and then reads what your program prints. Your job has three small steps.
- Create something that can read from standard input (the keyboard, or in the judge's case, a hidden test file).
- Read the whole line into a
Stringvariable. The word "whole" matters, because the sample contains spaces. - Print the greeting, then print the variable.
Dry run on the sample: the program reads the line Welcome to 30 Days of Code! and stores all 27 characters, spaces included, in inputString. It prints Hello, World. and moves to a new line. It then prints the stored text. The two lines match the expected output character for character, so the judge accepts it.
Solution in Java
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// nextLine() reads the whole line, spaces included
String inputString = scan.nextLine();
scan.close();
System.out.println("Hello, World.");
System.out.println(inputString);
}
}
A few pieces of Java here are worth understanding properly, because you will type them hundreds of times.
System.inis the standard input stream. On your laptop it is the keyboard. On HackerRank it is the test case file. Your code does not need to know which one it is.Scanneris a helper class fromjava.utilthat sits on top ofSystem.inand turns raw characters into useful values. You create it once withnew Scanner(System.in)and then ask it for data.nextLine()returns everything up to the end of the current line, as aString. Compare this withnext(), which stops at the first space and would give you onlyWelcome.System.out.println()prints its argument and then a newline. That newline is why the second print appears on a fresh line.scan.close()releases the scanner. It is good habit, but only close it after you have finished reading everything.
The old version of this post kept HackerRank's leftover "TODO" comment after the print statement had already been written. The logic was fine, the comment was just misleading, so it is gone here.
I also tested it with a line that has leading and repeated spaces ( spaces here). nextLine() kept every space, which is exactly what you want when the task says "print it unchanged".
Output
Hello, World.
Welcome to 30 Days of Code!
Complexity
Time is O(L), where L is the length of the input line, since Java has to read and print each character once. Extra space is also O(L) for the stored string.
Common mistakes
- Using
scan.next()instead ofscan.nextLine(). Your second line will only contain the first word, and the hidden test fails. - Typing the greeting slightly wrong, such as
Hello WorldorHello, World!. Judges compare text exactly, so a missing comma or the wrong final character is a wrong answer. - Printing only the greeting. The sample looks like it passes when you test with your own eyes, but the hidden test case uses a different input line.
- Naming the class something other than
Solutionon HackerRank. The file is compiled asSolution.java, so a public class with another name will not compile there.
Next in the series: Day 1: Data Types.
Tip: try to solve the problem yourself before reading the solution. Use the code as a reference to check your approach, and make sure you understand why it works.
More HackerRank solutions All coding problems MCQ practice
See everything in one place: Coding Problems and Solutions
Comments
Post a Comment