Java End Of File - HackerRank Solution
Java End Of File - HackerRank Solution
Problem :
"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.
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// Java End Of File - HackerRank Solution 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. */ // Java End Of File - HackerRank Solution START Scanner io = new Scanner(System.in); int i=0; while(io.hasNextLine()) { String s = io.nextLine(); if (s.contains("end-of-file")) { i++; System.out.println(i+" "+s); break; } else { i++; System.out.println(i+" " +s); } } // Java End Of File - HackerRank Solution END } } |
Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.