Skip to content

Instantly share code, notes, and snippets.

@amelieykw
Created June 25, 2018 19:53
Show Gist options
  • Save amelieykw/c3a290c4e0dd72f80edfdced81d15511 to your computer and use it in GitHub Desktop.
Save amelieykw/c3a290c4e0dd72f80edfdced81d15511 to your computer and use it in GitHub Desktop.
[Java End-Of-File || StdIn && StdOut] #Java #End-Of-File #EOF #StdIn #StdOut #Hackerrank
import java.io.*;
import java.util.*;
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 scan = new Scanner(System.in);
int lineNb = 0;
while(scan.hasNext()){
lineNb++;
String inputString = scan.nextLine();
System.out.println(lineNb+" "+inputString);
}
}
}

"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 lines of input until you reach EOF, then number and print all lines of content.

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

Input Format Read some unknown 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment