🗣️ Programming Language/Java

[Java] Class - Scanner(스캐너)

kimgoat 2024. 1. 5. 17:34

 

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