
Java Stdin and Stdout II looks like a copy of part I with two extra types: read an integer, a decimal number and a full sentence, then print them back in reverse order with labels. But it was designed to make you hit one of the most famous beginner bugs in Java. If you read the number with nextInt() or nextDouble() and then call nextLine(), the sentence comes back empty.
Almost every Java programmer has lost time to this at least once, often in a timed test. The fix is one line, but understanding why it happens matters more than memorising the fix, because the same thing shows up any time you mix token reading with line reading.
Problem
The input has three lines: an integer, a double, and a line of text that can contain spaces. Print three lines in this order: the text with the prefix String: , then the double with the prefix Double: , then the integer with the prefix Int: . Each value must appear exactly as it was read.
Input format: line 1 is an integer, line 2 is a double, line 3 is a string.
Output format: String: <text>, Double: <double>, Int: <integer> on three lines.
Constraints: none are given. The string is a single line.
Input:
42
3.1415
Welcome to HackerRank's Java tutorials!
Output:
String: Welcome to HackerRank's Java tutorials!
Double: 3.1415
Int: 42
Approach
Picture the input as one long stream of characters, with a newline character (\n) at the end of each line:
42\n3.1415\nWelcome to HackerRank's Java tutorials!\n
Scanner keeps a cursor that moves forward as you read.
nextInt()reads42. The cursor now sits just before the first\n.nextDouble()skips that newline, reads3.1415, and stops just before the second\n.- Now if you call
nextLine(), it returns "everything from the cursor up to the next newline". The cursor is already at a newline, so you get an empty string. This is the bug. - The fix: call
nextLine()once and ignore the result. That consumes the leftover\n. The nextnextLine()then reads the full sentence.
After that, print the three values in the required order: string first, integer last.
Solution in Java
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
// nextDouble() stops before the newline at the end of line 2.
// This call eats that leftover newline.
scan.nextLine();
// Now we are at the start of line 3, so we get the whole sentence
String s = scan.nextLine();
scan.close();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
The Scanner methods in plain words:
nextInt()/nextDouble()are token readers. They skip leading whitespace, read one "word", convert it to a number, and stop. They never consume the whitespace that follows.nextLine()is a line reader. It returns all characters up to the next newline and then moves past that newline. It does not skip anything first, which is why it can return an empty string.next()reads one word only. Using it for the third line would give youWelcomeand drop the rest.
The code that was published here before did not compile. It read only the integer and then printed s and d, which were never declared. The version above reads all three values and handles the leftover newline.
To confirm the bug is real, I also ran a version without the extra nextLine(). On the sample it printed String: with nothing after it, while the other two lines looked fine. That is what makes this bug sneaky: two out of three lines are correct. The fixed version printed all three lines correctly on the sample and on a second test (7, 2.0, hi).
A small note on how doubles print: Java prints 3.1415 back as 3.1415 and 2.0 as 2.0. If the input were 2, it would print 2.0, since a double always shows a decimal part. The judge's tests use inputs where this matches.
Output
String: Welcome to HackerRank's Java tutorials!
Double: 3.1415
Int: 42
Complexity
O(L) time and space, where L is the length of the sentence. Everything else is constant work.
Common mistakes
- Skipping the extra
scan.nextLine(), sosis empty and the first output line is justString:. - Using
scan.next()for the sentence, which reads only the first word. - Printing in input order (Int, Double, String) instead of the order asked for.
- Missing the space after the colon in the labels, for example
String:Welcome.
Before this one: Java Stdin and Stdout I. Next in the Java track: Java Output Formatting.
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