logo

Java 스캐너 next() 메서드

next()는 사용 중인 스캐너에서 다음 완전한 토큰을 찾아 반환하는 Java Scanner 클래스의 메서드입니다. Java Scanner next() 메소드에는 매개변수에 따라 구별할 수 있는 세 가지 유형이 있습니다. 이것들은:

  • Java 스캐너 next() 메서드
  • Java Scanner next(문자열 패턴) 메소드
  • Java Scanner next(패턴 패턴) 메소드

1. 자바 스캐너 next() 메소드

사용 중인 스캐너에서 다음 완전한 토큰을 가져오는 데 사용되는 Scanner 클래스 메서드입니다. 완전한 토큰 앞뒤에는 구분 기호 패턴과 일치하는 입력이 옵니다.

2. Java Scanner next(문자열 패턴) 방법

지정된 문자열에서 구성된 패턴과 일치하는 경우 다음 토큰을 반환하는 Scanner 클래스 메서드입니다.

3. Java Scanner next(패턴 패턴) 방법

지정된 패턴과 일치하면 다음 토큰을 반환하는 Scanner 클래스 메서드입니다.

통사론

다음은 선언문이다. 다음() 방법:

 public String next() public String next(String pattern) public String next(Pattern pattern) 

매개변수

데이터 형식 매개변수 설명 필수/선택
무늬 스캔할 패턴을 지정하는 문자열입니다. 필수의
무늬 무늬 지정된 문자열을 스캔하는 패턴입니다. 필수의

보고

next() 메소드는 다음 완전한 토큰을 반환합니다.

예외

NoSuchElementException - 더 이상 토큰이 발견되지 않으면 이 예외가 발생합니다.

IllegalStateException - Scanner가 닫힌 후 호출이 완료되면 이 예외가 발생합니다.

호환성 버전

자바 1.5 이상

실시예 1

 import java.util.*; public class ScannerNextExample1 { public static void main(String[] args) { System.out.print('Enter full name: '); //Create scanner object and read the value from the console Scanner scan = new Scanner(System.in); //Read the first token String firstName = scan.next(); //Read the second token String lastName = scan.next(); //Print the token values read by Scanner object System.out.println('First Name is: '+firstName); System.out.println('Last Name is: '+lastName); scan.close(); } } 

산출:

 Enter full name: Hritik Roshan First Name is: Hritik Last Name is: Roshan 

실시예 2

 import java.io.File; import java.io.FileNotFoundException; import java.util.*; public class ScannerNextExample2 { public static void main(String args[]) throws FileNotFoundException{ //Declare File object File file = new File('/home/javatpoint/Desktop/ABHISHEK/AngularJS/Index/abc.txt'); //Initialize the scanner Scanner scan = new Scanner(file); // iterate through the file line by line while(scan.hasNextLine()){ //Print the contents of a file by line System.out.println(scan.next()); } scan.close(); } } 

산출:

 hasNextLine public boolean hasNextLine() IllegalStateException 

실시예 3

 import java.util.*; public class ScannerNextExample3 { public static void main(String args[]) { String s = 'Facebook.com 
 JavaTpoint.com 22 60.0'; //Create a new scanner with the specified String Object Scanner scanner = new Scanner(s); //Find the next token and print it System.out.print('Token Value1 ' + scanner.next()); System.out.print('
Token value2: ' + scanner.next()); scanner.close(); } } 

산출:

 Token Value1 Facebook.com Token value2: JavaTpoint.com 

실시예 4

 import java.util.*; public class ScannerNextExample4 { public static void main(String args[]) { //Initialize Scanner object Scanner scan = new Scanner('22 313 45 87'); //Intialize the String pattern String pattern = '[0-9]*'; //Print the tokenized Strings while(scan.hasNext()){ System.out.println('tokenized Strings: '+scan.next(pattern)); } scan.close(); } } 

산출:

 tokenized Strings: 22 tokenized Strings: 313 tokenized Strings: 45 tokenized Strings: 87 

실시예 5

 import java.util.*; import java.util.regex.Pattern; public class ScannerNextExample5 { public static void main(String args[]){ String str = 'JavaTpoint Hello World!'; Scanner scanner = new Scanner(str); //Check if next token matches the pattern and print it System.out.println('' + scanner.next(Pattern.compile('.....point'))); //Check if next token matches the pattern and print it System.out.println('' + scanner.next(Pattern.compile('..llo'))); scanner.close(); } } 

산출:

 JavaTpoint Hello