Scanner: java.util 패키지 내에 존재하는 자바의 기본 입력 클래스
Method of Class Scanner
Type(타입) | Method(메소드) | Description(설명) |
byte | nextByte(), nextByte(int radix) |
Scans the next token of the input as abyte. |
short | nextShort(), nextShort(int radix) |
Scans the next token of the input as a short. |
int | nextInt(), nextInt(int radix) |
Scans the next token of the input as an int. |
long | nextLong(), nextLobg(int radix) |
Scans the next token of the input as a long. |
float | nextFloat() | Scans the next token of the input as a float. |
double | nextDouble() | Scans the next token of the input as a double. |
boolean | nextBoolean() | Scans the next token of the input into a boolean value and returns that value. |
String | next() | Finds and returns the next complete token from this scanner. |
String | nextLine() | Advances this scanner past the current line and returns the input that was skipped. |
boolean | hasNextByte() | Returns true if the next token in this scanner's input can be interpreted as a byte value in the default radix using the nextByte() method. |
boolean | hasNextShort() | Returns true if the next token in this scanner's input can be interpreted as a short value in the default radix using the nextShort() method. |
boolean | hasNextInt() | Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. |
boolean | hasNextLong() | Returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the nextLong() method. |
boolean | hasNextFloat() | Returns true if the next token in this scanner's input can be interpreted as a float value using the nextFloat() method. |
boolean | hasNextDouble() | Returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method. |
boolean | hasNextBoolean() | Returns true if the next token in this scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false". |
boolean | hasNext() | Returns true if this scanner has another token in its input. |
boolean | hasNextLine() | Returns true if there is another line in the input of this scanner. |
import java.util.Scanner; // java.util 패키지에 존재하는 클래스를 사용하므로 해당 패키지를 import
public class TestScanner{
public static void main(String[] args){
Scanner sc = new Scanner(System.in); // Scanner 객체 생성
byte b = sc.nextByte();
short s = sc.nextShort();
int i = sc.nextInt();
long l = sc.nextLong();
float f = sc.nextFloat();
double d = sc.nextDouble();
boolean b = sc.nextBoolean();
String str1 = sc.next(); // 토큰(Spacebar, tab, Enter)을 기준으로 한 단어 읽음
String str2 = sc.nextLine(); // 개행(Enter)을 기준으로 한 줄 읽음
System.out.println(b);
System.out.println(s);
System.out.println(i);
System.out.println(l);
System.out.println(f);
System.out.println(d);
System.out.println(b);
System.out.println(str1);
System.out.println(str2);
}
}
<참조>
Class Scanner, [Website], 검색경로 - https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
'🗣️ Programming Language > Java' 카테고리의 다른 글
[Java] 자바 변수와 관련된 주요 키워드 정리 (0) | 2024.10.19 |
---|---|
[Java] 생명 주기와 접근 범위에 따른 변수의 종류 (0) | 2024.10.18 |
[Java] ArrayList 구현 - get(), add(), remove(), toString() (0) | 2024.01.06 |
[Java] 조건문 - 삼항 연산자(ternary operator) (0) | 2024.01.04 |