Algorithm


Problem Name: Java End-of-file

Problem Link: https://www.hackerrank.com/challenges/java-end-of-file/problem?isFullScreen=true

In this HackerRank Functions in Java programming problem solution,

"In computing, End Of File (commonly abbreviated EOF) is a condition in a computer operating system where no more data can be read from a data source." — (Wikipedia: End-of-file)

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.

 

 

 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


import java.util.Scanner;

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

		int testCase = 0;
		while(input.hasNext())
			System.out.println(++testCase + " " + input.nextLine());
	}
}
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Java Datatypes in Java solution in Hackerrank - Hacerrank solution Java
Next
[Solved] Java Static Initializer Block Java solution in Hackerrank - Hacerrank solution Java