📜  Java中的 Scanner skip() 方法和示例

📅  最后修改于: 2022-05-13 01:54:26.260000             🧑  作者: Mango

Java中的 Scanner skip() 方法和示例

跳过(图案图案)

Java.util.Scanner类的skip(Pattern pattern)方法跳过与指定模式匹配的输入,忽略分隔符。如果指定模式的锚定匹配成功,该函数将跳过输入。

句法:

public Scanner skip(Pattern pattern)

参数:该函数接受一个强制参数模式,该模式指定一个字符串作为要跳过的模式。

返回值:函数返回此扫描仪

异常:此方法引发以下异常:

  • NoSuchElementException : 未找到指定模式时
  • 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
    {
  
        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

跳过(字符串模式)

Java.util.Scanner类的skip(String pattern)方法跳过与从指定字符串构造的模式匹配的输入。 skip(pattern) 和 skip(Pattern.compile(pattern)) 在被调用时的行为方式完全相同。

句法:

public Scanner skip(String pattern)

参数:该函数接受一个强制参数字符串模式,它指定一个表示要跳过的模式的字符串

返回值:函数返回此扫描仪

异常:当此扫描仪关闭时,此方法抛出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
    {
  
        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

方案二:演示 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: Java Java.util.regex.Pattern)