Java Scanner skip()方法及示例

Java Scanner skip()方法及示例

skip(Pattern pattern)

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

语法

public Scanner skip(Pattern 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)

参数: 该函数接受一个强制性参数字符串 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)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程