I am doing Java exercises from HackerRank.com and bit confused about this problem.

Here is my code that pass the test case.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

     // String input = in.nextLine();

        int counter = 1;
        while (in.hasNext()) {
            System.out.println(counter + " " + in.nextLine());

We are suppose to print number of lines of input from Scanner, until line of input contains a empty String. The line of inputs are automatically generated by the HackerRank website. For test case, three lines of input will be generated.

Test case input

Hello World

I am a file

Read me until end-of-file.

Expected output

1 Hello World

2 I am a file

3 Read me until end-of-file.

However, whenever I uncomment out the

String input = in.nextLine();

My output only shows first 2 lines instead of all 3. I thought I need that line of code in order to type a line of input. For example,

Scanner sc = new Scanner(System.in);
System.out.println("Enter your name.");
String name = sc.nextLine();

Also when I write

String input = in.next();

instead of nextLine() code, my output shows all 3 lines but first word "Hello" is omitted.

in.next(); output

1 World

2 I am a file

3 Read me until end-of-file.

What I want to know is why I don't need String input = in.nextLine(); to pass the test case.


closed as off-topic by Tunaki, forsvarir, Phrancis, Hosch250, Jamal Aug 17 '16 at 21:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Tunaki, forsvarir, Phrancis, Hosch250, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. \$\endgroup\$ – Phrancis Aug 17 '16 at 20:29

The nextLine method is included in your print, so when you include it at the start then the first line ("Hello World") is stored in input and it is not touched again. Then, when the nextLine in the println method runs, it checks for the next line's content ("I am a file"), and prints "1 I am a file" because it is the first iteration of the loop. It then works successfully in getting and printing the rest of the lines of input.

The next method gets the next word in this context, so using it would cause "Hello" to be stored in input and, as input is never used again, forgotten. nextLine then gets the rest of the line's content and continues successfully for other input lines.

Neither methods are required to start with because the call to nextLine within println runs successfully for each iteration.


Not the answer you're looking for? Browse other questions tagged or ask your own question.