#8 Java End-of-file

HackerRank/HR-Java 2015. 11. 23. 10:32 by 후뤼한잉여

https://www.hackerrank.com/challenges/java-end-of-file


Problem Statement

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)

Sometimes you don't know how many lines are there in a file and you need to read the file until EOF or End-of-file. In this problem you need to read a file until EOF and print the contents of the file. You must take input from stdin(System.in).

Hints: One way to do this is to use hasNext() function in java scanner class.

Input Format

Each line will contain a non-empty string. Read until EOF.

Output Format

For each line, print the line number followed by a single space and the line content.

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. 

Copyright © 2015 HackerRank.


파일을 기본 입력으로 부터 받아 파일의 끝까지 아래와 같이 출력하시오.


보낸 답)

import java.util.Scanner;


public class Solution {


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        

        int i = 0;

        while(sc.hasNext()) {

            i++;

            System.out.println(i + " " + sc.nextLine());

        }

    }

}



'HackerRank > HR-Java' 카테고리의 다른 글

#9 Java Static Initializer Block  (0) 2015.11.23
#6 Java Loops  (0) 2015.11.18
#5 Java Output Formatting  (0) 2015.11.17
#4 Java Stdin and Stdout 2  (0) 2015.11.17
#3 Java If-Else  (0) 2015.11.17
Nav