Java End-of-file | HackerRank Solution

Hello coders, today we are going to solve Java End-of-file HackerRank Solution.

Java End-of-file

Problem

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

 Hello world
 I am a file
 Read me until end-of-file.

Sample Output

 1 Hello world
 2 I am a file
 3 Read me until end-of-file.

Solution – Java End-of-file

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
              Scanner sc = new Scanner(System.in);
               int c =1;
               while(sc.hasNext()){
               String s = sc.nextLine();
               System.out.println(c+" "+s);
               c++;
            }
        }
   }


Disclaimer: The above Problem ( Java End-of-file ) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

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