Java End-of-file : Hacker Rank Solution : Digit Wood

Java
,

The challenge here is to read n lines of input until you reach EOF, then number and print all n lines of content.

Hint: Java’s Scanner.hasNext() method is helpful for this problem.

Input Format

Read some unknown n lines of input from stdin(System.in) until you reach EOF; each line of input contains a non-empty String.

Output Format

For each line, print the line number, followed by a single space, and then the line content received as input.

Sample Input

Sample Output

SOLUTION:

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

public class Solution {

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

public class Solution {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        int count = 0;
        while (s.hasNextLine()) {

            count++;

            System.out.println(count + " " + s.nextLine());

        }
    }
}

FOLLOW FOR MORE QUESTIONS AND SOLUTIONS | DIGIT WOOD

Leave a Reply

Your email address will not be published. Required fields are marked *