當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Scanner skip()用法及代碼示例


skip(Pattern pattern)

java.util.Scanner類的skip(Pattern pattern)方法跳過與指定模式匹配的輸入,忽略定界符。如果指定模式的錨定匹配成功,則該函數將跳過輸入。

用法:

public Scanner skip(Pattern pattern)

參數:該函數接受強製性參數模式,該模式將字符串指定為要跳過的模式。


返回值:函數返回此掃描儀

異常:此方法引發以下異常:

  • NoSuchElementException:當找不到指定的模式時
  • IllegalStateException:關閉此掃描儀時

以下示例程序旨在說明上述函數:

示例1:

// Java program to illustrate the 
// skip() method of Scanner class in Java 
  
import java.util.*; 
import java.util.regex.Pattern; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
  
        String s = "GeeksForGeeks - "
                   + "A Computer Science Portal for Geeks"; 
  
        System.out.println("String trying to get input:\n"
                           + s); 
  
        // create a new scanner with 
        // the specified String Object 
        Scanner scanner = new Scanner(s); 
  
        // skip the word that 
        // matches with the pattern ..eks 
        System.out.println("Skipping 5 letter words"
                           + " that ends with 'eks'\n"); 
  
        scanner.skip(Pattern.compile("..eks")); 
  
        // print a line of the scanner 
        System.out.println("Input Scanner String: \n"
                           + scanner.nextLine()); 
  
        // close the scanner 
        scanner.close(); 
    } 
}
輸出:
String trying to get input:
GeeksForGeeks - A Computer Science Portal for Geeks
Skipping 5 letter words that ends with 'eks'

Input Scanner String: 
ForGeeks - A Computer Science Portal for Geeks

示例2:演示NoSuchElementException

// Java program to illustrate the 
// skip() method of Scanner class in Java 
  
import java.util.*; 
import java.util.regex.Pattern; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
  
        try { 
            String s = "GeeksForGeeks - "
                       + "A Computer Science Portal for Geeks"; 
  
            System.out.println("String trying to get input:\n"
                               + s); 
  
            // create a new scanner with 
            // the specified String Object 
            Scanner scanner = new Scanner(s); 
  
            // skip the word that 
            // matches with the pattern and 
            System.out.println("Skipping 3 letter words"
                               + " and\n"); 
  
            scanner.skip(Pattern.compile("and")); 
  
            // print a line of the scanner 
            System.out.println("Input Scanner String: \n"
                               + scanner.nextLine()); 
  
            // close the scanner 
            scanner.close(); 
        } 
  
        catch (Exception e) { 
            System.out.println("Exception thrown: " + e); 
        } 
    } 
}
輸出:
String trying to get input:
GeeksForGeeks - A Computer Science Portal for Geeks
Skipping 3 letter words and

Exception thrown: java.util.NoSuchElementException

示例3:演示IllegalStateException

// Java program to illustrate the 
// skip() method of Scanner class in Java 
  
import java.util.*; 
import java.util.regex.Pattern; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
  
        try { 
            String s = "GeeksForGeeks - "
                       + "A Computer Science Portal for Geeks"; 
  
            System.out.println("String trying to get input:\n"
                               + s); 
  
            // create a new scanner with 
            // the specified String Object 
            Scanner scanner = new Scanner(s); 
  
            // close the scanner 
            scanner.close(); 
            System.out.println("Scanner Closed"); 
  
            // skip the word that 
            // matches with the pattern and 
            System.out.println("Trying to Skip 3 letter words"
                               + " and\n"); 
  
            scanner.skip(Pattern.compile("and")); 
  
            // print a line of the scanner 
            System.out.println("Input Scanner String: \n"
                               + scanner.nextLine()); 
        } 
  
        catch (Exception e) { 
            System.out.println("Exception thrown: " + e); 
        } 
    } 
}
輸出:
String trying to get input:
GeeksForGeeks - A Computer Science Portal for Geeks
Scanner Closed
Trying to Skip 3 letter words and

Exception thrown: java.lang.IllegalStateException: Scanner closed

skip(String pattern)

java.util.Scanner類的skip(String pattern)方法將跳過與從指定字符串構造的模式匹配的輸入。 skip(pattern)和skip(Pattern.compile(pattern))在調用時的行為完全相同。


用法:

public Scanner skip(String pattern)

參數:該函數接受強製性參數字符串模式,該參數指定一個字符串,該字符串表示要跳過的模式

返回值:函數返回此掃描儀

異常:關閉此掃描器時,此方法引發IllegalStateException

以下示例程序旨在說明上述函數:

示例1:

// Java program to illustrate the 
// skip() method of Scanner class in Java 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
  
        String s = "GeeksForGeeks - "
                   + "A Computer Science Portal for Geeks"; 
  
        System.out.println("String trying to get input:\n"
                           + s); 
  
        // create a new scanner with 
        // the specified String Object 
        Scanner scanner = new Scanner(s); 
  
        // skip the word that 
        // matches with the pattern ..eks 
        System.out.println("Skipping 5 letter words"
                           + " that ends with 'eks'\n"); 
  
        scanner.skip("..eks"); 
  
        // print a line of the scanner 
        System.out.println("Input Scanner String: \n"
                           + scanner.nextLine()); 
  
        // close the scanner 
        scanner.close(); 
    } 
}
輸出:
String trying to get input:
GeeksForGeeks - A Computer Science Portal for Geeks
Skipping 5 letter words that ends with 'eks'

Input Scanner String: 
ForGeeks - A Computer Science Portal for Geeks

示例2:演示IllegalStateException

// Java program to illustrate the 
// skip() method of Scanner class in Java 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
  
        try { 
            String s = "GeeksForGeeks - "
                       + "A Computer Science Portal for Geeks"; 
  
            System.out.println("String trying to get input:\n"
                               + s); 
  
            // create a new scanner with 
            // the specified String Object 
            Scanner scanner = new Scanner(s); 
  
            // close the scanner 
            scanner.close(); 
            System.out.println("Scanner Closed"); 
  
            // skip the word that 
            // matches with the pattern and 
            System.out.println("Trying to Skip 3 letter words"
                               + " and\n"); 
  
            scanner.skip("and"); 
  
            // print a line of the scanner 
            System.out.println("Input Scanner String: \n"
                               + scanner.nextLine()); 
        } 
  
        catch (Exception e) { 
            System.out.println("Exception thrown: " + e); 
        } 
    } 
}
輸出:
String trying to get input:
GeeksForGeeks - A Computer Science Portal for Geeks
Scanner Closed
Trying to Skip 3 letter words and

Exception thrown: java.lang.IllegalStateException: Scanner closed

參考: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#skip(java.util.regex.Pattern)



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Scanner skip() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。