IT/Programming / / 2023. 4. 21. 17:44

[JAVA] nextLine 사용 예제

반응형
package dayday6;

import java.util.Scanner;

public class test03 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("enter numerical value");    
		int option;
		Scanner input = new Scanner(System.in);
		try {
		    option = Integer.parseInt(input.nextLine());
		} catch (NumberFormatException e) {
		    e.printStackTrace();
		}
		
		System.out.println("enter 1st string"); 
		String string1 = input.nextLine();//read 1st string (this is skipped)
		System.out.println("enter 2nd string");
		String string2 = input.nextLine();//read 2nd string (this appears right after reading numerical value)
	}
}
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유