조건문에서 nextLine() 로 문자열을 입력받고 더 이상의 입력할 문자가 없을 때, Enter키를 입력하면 끝내는 간단한 방법이다.

의외로 nextLine() 후 엔터를 쳤을 때 끝내는 방법을 모르는 사람이 많아서 참고하시길..

 

 

1. CLASS

 

import java.util.Scanner;

 

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


    String b = "";


    while(true){
      String a = sc.nextLine() + "\n";
      
      if(a.equals("\n")){
        System.out.println(b);
        break;
      }

 

      b += a;
    }
  }
}

 

 

2. RUN

 

1 a b c d
2 e f g h
3 i j k l
4 m n o p

 

1 a b c d
2 e f g h
3 i j k l
4 m n o p