[HackerRank]-End-of-file

세번 째 해커랭크 문제 기초이다.

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.


예시 설명 문자열을 입력받고각 문자열 맨앞에 숫자를 매기면 된다.


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 input = new Scanner(System.in);

        String output;
        int i = 1;
        while(input.hasNext()){
            output = input.nextLine();
            System.out.println(i+ " "+output);
            i++;
        }
    }
}

설명

while문에 hasNext()를 써서 입력받은것(다음 값)이 더 있으면 while문을 실행한다.

요약

HasNext()를 알아두자.