Java Scanner skip() Method



Description

The Java Scanner skip(Pattern pattern) method skips input that matches the specified pattern, ignoring delimiters. This method will skip input if an anchored match of the specified pattern succeeds.If a match to the specified pattern is not found at the current position, then no input is skipped and a NoSuchElementException is thrown.

Declaration

Following is the declaration for java.util.Scanner.skip() method

public Scanner skip(Pattern pattern)

Parameters

pattern − a string specifying the pattern to skip over

Return Value

This method returns this scanner

Exception

  • NoSuchElementException − if the specified pattern is not found

  • IllegalStateException − if this scanner is closed

Java Scanner skip(String pattern) Method

Description

The Java Scanner skip(String pattern) method skips input that matches a pattern constructed from the specified string.An invocation of this method of the form skip(pattern) behaves in exactly the same way as the invocation skip(Pattern.compile(pattern)).

Declaration

Following is the declaration for java.util.Scanner.skip(String pattern) method

public Scanner skip(String pattern)

Parameters

pattern − a string specifying the pattern to skip over

Return Value

This method returns this scanner

Exception

IllegalStateException − if this scanner is closed

Skipping a Token based on Pattern of a Scanner on a String Example

The following example shows the usage of Java Scanner skip(Pattern pattern) method to skip a pattern in a given string. We've created a scanner object using a given string. We've retrieved a stream using findAll(Pattern) method. A stream is then iterated to print the result. Then we printed the string using scanner.nextLine() method.

package com.tutorialspoint;

import java.util.Scanner;
import java.util.regex.Pattern;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6.0 true ";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // skip the word that matches the pattern ..llo
      scanner.skip(Pattern.compile("..llo"));

      // print a line of the scanner
      System.out.println(scanner.nextLine());

      // close the scanner
      scanner.close();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

 World! 3 + 3.0 = 6.0 true 

Skipping a Token based on String of a Scanner on a String Example

The following example shows the usage of Java Scanner skip(String pattern) method to skip a pattern in a given string. We've created a scanner object using a given string. We've retrieved a stream using findAll(String) method. A stream is then iterated to print the result. Then we printed the string using scanner.nextLine() method.

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6.0 true ";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // skip the word that matches the pattern ..llo
      scanner.skip("..llo");

      // print a line of the scanner
      System.out.println(scanner.nextLine());

      // close the scanner
      scanner.close();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

 World! 3 + 3.0 = 6.0 true 

Skipping a Token based on Pattern of a Scanner on User Input Example

The following example shows the usage of Java Scanner skip(String pattern) method to skip a pattern in a given string. We've created a scanner object using System.in class. We've retrieved a stream using findAll(String) method. A stream is then iterated to print the result. Then we printed the string using scanner.nextLine() method.

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      // create a new scanner with the System input
      Scanner scanner = new Scanner(System.in);

      // skip the word that matches the pattern ..llo
      scanner.skip("..llo");

      // print a line of the scanner
      System.out.println(scanner.nextLine());

      // close the scanner
      scanner.close();
   }
}

Output

Let us compile and run the above program, this will produce the following result − (We've entered Hello World.)

Hello World
 World
java_util_scanner.htm
Advertisements