Scanner 기본 사용 방법
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
- System.in은 키보드와 연결된 표준 입력 스트림(standard input stream)이다.
- sc.nextInt(); 는 키보드로 입력한 int형 정숫값을 읽어 들이는 부분이다.
Scanner 클래스에 포함되어 있는 next 메서드
메서드 | 자료형 | 입력값의 범위 |
---|
nextBoolean() | boolean | true 또는 false |
nextByte() | byte | -128 ~ +127 |
nextShort() | short | -32768 ~ +32767 |
nextInt() | int | -2147483648 ~ +2147483647 |
nextLong() | long | -9223372036854775808 ~ +9223372036854775807 |
nextFloat() | float | +-3.4028247E+38 ~ +-4.94065645841246544E-324 |
nextDouble() | double | +-1.79769313486231507E+378 ~ +-4.94065645841246544E-324 |
next() | String | 문자열(공백, 줄 바꿈 문자로 구분) |
nextLine() | String | 문자열 1줄 |