当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。