java scanner
扫描仪类skip()方法 (Scanner Class skip() method)
Syntax:
句法:
public Scanner skip(Pattern patt);
public Scanner skip(String patt);
skip() method is available in java.util package.
skip()方法在java.util包中可用。
skip(Pattern patt) method is used to skip input that meets the given pattern, avoiding delimiters.
skip(Pattern patt)方法用于跳过符合给定模式的输入,避免使用分隔符。
skip(String patt) method is used to skip input that meets the pattern formed from the given string (patt).
skip(String patt)方法用于跳过符合由给定字符串(patt)形成的模式的输入。
These methods may throw an exception at the time of skipping input that meets the given pattern.
这些方法在跳过符合给定模式的输入时可能会引发异常。
- NoSuchElementException: This exception may throw when the given pattern does not exist.NoSuchElementException :如果给定模式不存在,则可能引发此异常。
- IllegalStateException: This exception may throw when this Scanner is not opened.IllegalStateException :如果未打开此扫描器,则可能引发此异常。
These are non-static methods and it is accessible with the class object only and if we try to access these methods with the class name then we will get an error.
这些是非静态方法,只能通过类对象访问,如果尝试使用类名称访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first case, skip(Pattern patt),
在第一种情况下,请跳过(Pattern patt) ,
- Pattern patt – represents the pattern to skip.
- 模式patt –表示要跳过的模式。
In the second case, skip(String patt),
在第二种情况下, skip(String patt) ,
- String patt – represents a string denoting the pattern to skip.
- 字符串模式–表示表示要跳过的模式的字符串。
Return value:
返回值:
In both the cases, the return type of the method is Scanner, it retrieves this Scanner object.
在这两种情况下,方法的返回类型均为Scanner ,它将检索此Scanner对象。
Example 1:
范例1:
// Java program to demonstrate the example
// of skip() method of Scanner
import java.util.*;
import java.util.regex.*;
public class Skip {
public static void main(String[] args) {
String str = "Java Programming! 3 * 8= 24";
// Instantiates Scanner
Scanner sc = new Scanner(str);
// By using skip(Pattern) method
// is to skip that meets the given
// pattern
sc.skip(Pattern.compile(".ava"));
System.out.println("sc.skip(): " + sc.nextLine());
// Scanner closed
sc.close();
}
}
Output
输出量
sc.skip(): Programming! 3 * 8= 24
Example 2:
范例2:
import java.util.*;
import java.util.regex.*;
public class Skip {
public static void main(String[] args) {
String str = "Java Programming! 3 * 8= 24";
// Instantiates Scanner
Scanner sc = new Scanner(str);
// By using skip(String) method
// is to skip that meets the given
// pattern constructed from the given
// String
sc.skip("Java");
System.out.println("sc.skip(Java): " + sc.nextLine());
// Scanner closed
sc.close();
}
}
Output
输出量
sc.skip(Java): Programming! 3 * 8= 24
翻译自: https://www.includehelp.com/java/scanner-skip-method-with-example.aspx
java scanner