> Java > java지도 시간 > 본문

자바 스캐너 클래스

WBOY
풀어 주다: 2024-08-30 16:10:08
원래의
337명이 탐색했습니다.

Java에서 Scanner는 문자열과 int, double 등과 같은 다양한 기본 유형의 입력을 얻는 데 사용되는 클래스입니다. scanner 클래스는 java 패키지에 있습니다. 이는 Object 클래스를 확장하고 Closeable 및 Iterator 인터페이스를 구현합니다. 입력은 공백 구분 기호를 사용하여 클래스로 구분됩니다. 다음 섹션에서는 Java Scanner 클래스의 선언, 구문, 작업 및 기타 여러 측면에 대해 설명합니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

Java 스캐너 클래스 선언

Java Scanner 클래스는 아래와 같이 선언됩니다.

public final class Scanner extends Object implements Iterator<String>
로그인 후 복사

구문:

스캐너 클래스는 Java 프로그램에서 아래 구문으로 사용할 수 있습니다.

Scanner sc = new Scanner(System.in);
로그인 후 복사

여기서 sc는 스캐너 개체이고 System.in은 입력이 다음과 같을 것이라고 Java에 알려줍니다.

Java 스캐너 클래스는 어떻게 작동하나요?

이제 Scanner 클래스가 어떻게 작동하는지 살펴보겠습니다.

  • java.util.Scanner 클래스 또는 전체 패키지 java.util을 가져옵니다.
import java.util.*;
import java.util.Scanner;
로그인 후 복사
  • 구문에 표시된 대로 스캐너 개체를 생성합니다.
Scanner s = new Scanner(System.in);
로그인 후 복사
  • int, char 또는 string 유형을 선언하세요.
int n = sc.nextInt();
로그인 후 복사
  • 요구사항에 따라 입력에 대한 작업을 수행합니다.

건축자

Java의 스캐너 클래스에는 다양한 생성자가 있습니다. 그들은:

  • 스캐너(파일 src): 언급된 파일에서 값을 생성하는 새로운 스캐너가 구성됩니다.
  • Scanner(File src, String charsetName): 언급된 파일에서 스캔된 값을 생성하는 새로운 스캐너가 구성됩니다.
  • 스캐너(InputStream src): 언급된 입력 스트림에서 값을 생성하는 새로운 스캐너가 구성됩니다.
  • Scanner(InputStream src, String charsetName): 언급된 입력 스트림에서 값을 생성하는 새로운 스캐너가 구성됩니다.
  • 스캐너(Path src): 언급된 파일에서 값을 생성하는 새로운 스캐너가 구성됩니다.
  • Scanner(Path src, String charsetName): 언급된 파일에서 값을 생성하는 새로운 스캐너가 구성됩니다.
  • 스캐너(읽기 가능한 src): 언급된 소스에서 가치를 생성하는 새로운 스캐너가 구성됩니다.
  • Scanner(ReadableByteChannel src): 언급된 채널에서 가치를 생성하는 새로운 스캐너가 구성됩니다.
  • Scanner(ReadableByteChannel src, String charsetName): 언급된 채널에서 값을 생성하는 새로운 스캐너가 구성됩니다.
  • 스캐너(String src): 언급된 문자열에서 값을 생성하는 새로운 스캐너가 구성됩니다.

Java 스캐너 클래스의 메소드

다음은 다양한 기능을 수행하는 방법입니다.

  • close(): Scanner gets closed on calling this method.
  • findInLine(Pattern p): Next occurrence of the mentioned pattern p will be attempted to find out, ignoring the delimiters.
  • findInLine(String p): The next occurrence of the pattern that is made from the string will be attempted to find out, ignoring the delimiters.
  • delimiter(): Pattern that is currently used by the scanner to match delimiters will be returned.
  • findInLine(String p): The next occurrence of the pattern that is made from the string will be attempted to find out, ignoring the delimiters.
  • findWithinHorizon(Pattern p, int horizon): Tries to identify the mentioned pattern’s next occurrence.
  • hasNext(): If the scanner has another token in the input, true will be returned.
  • findWithinHorizon(String p, int horizon): Tries to identify the next occurrence of the mentioned pattern made from the string p, ignoring delimiters.
  • hasNext(Pattern p): If the next token matches the mentioned pattern p, true will be returned.
  • hasNext(String p): If the next token matches the mentioned pattern p made from the string p, true will be returned.
  • next(Pattern p): Next token will be returned, which matches the mentioned pattern p.
  • next(String p): Next token will be returned, which matches the mentioned pattern p made from the string p.
  • nextBigDecimal(): Input’s next token will be scanned as a BigDecimal.
  • nextBigInteger(): Input’s next token will be scanned as a BigInteger.
  • nextBigInteger(int rad): Input’s next token will be scanned as a BigInteger.
  • nextBoolean(): Input’s next token will be scanned as a Boolean, and it will be returned.
  • nextByte(): Input’s next token will be scanned as a byte.
  • nextByte(int rad): Input’s next token will be scanned as a byte.
  • nextDouble(): Input’s next token will be scanned as a double.
  • nextFloat(): Input’s next token will be scanned as a float.
  • nextInt(): Input’s next token will be scanned as an integer.
  • nextInt(int rad): Input’s next token will be scanned as an integer.
  • nextLong(int rad): Input’s next token will be scanned as a long.
  • nextLong(): Input’s next token will be scanned as a long.
  • nextShort(int rad): Input’s next token will be scanned as a short.
  • nextShort(): Input’s next token will be scanned as a short.
  • radix(): The default radix of the scanner will be returned.
  • useDelimiter(Pattern p): Delimiting pattern of the scanner will be set to the mentioned pattern.
  • useDelimiter(String p): Delimiting pattern of the scanner will be set to the mentioned pattern made from the string p.
  • useRadix(int rad): The scanner’s default radix will be set to the mentioned radix.
  • useLocale(Locale loc): The locale of the scanner will be set to the mentioned locale.

Examples of Java Scanner Class

Now, let us see some sample programs of the Scanner class in Java.

Example #1

Code:

import java.util.Scanner;
class ScannerExample {
public static void main(String[] args) {
//create object of scanner
Scanner sc = new Scanner(System.in);
System.out.println("Enter the username");
String obj = sc.nextLine();
System.out.println("The name you entered is: " + obj);
}
}
로그인 후 복사

Output:

자바 스캐너 클래스

On executing the program, the user will be asked to enter a name. As the input is a string, the nextLine() method will be used. Once it is entered, a line will be printed displaying the name user given as input.

Example #2

Code:

import java.util.Scanner;
class ScannerExample {
public static void main(String[] args) {
//create object of scanner
Scanner sc = new Scanner(System.in);
System.out.println("Enter age");
int obj = sc.nextInt();
System.out.println("The age you entered is: " + obj);
}
}
로그인 후 복사

Output:

자바 스캐너 클래스

On executing the program, the user will be asked to enter the age. As the input is a number, the nextInt() method will be used. Once it is entered, a line will be printed displaying the age user given as input.

위 내용은 자바 스캐너 클래스의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!