saltar (patrón de patrón)
El método skip(Pattern pattern) de la clase java.util.Scanner omite la entrada que coincide con el patrón especificado, ignorando los delimitadores. La función omite la entrada si una coincidencia anclada del patrón especificado tiene éxito.
Sintaxis:
public Scanner skip(Pattern pattern)
Parámetros: la función acepta un patrón de parámetro obligatorio que especifica una string como patrón que se debe omitir.
Valor devuelto: la función devuelve este escáner
Excepciones: este método arroja las siguientes excepciones:
- NoSuchElementException : cuando no se encuentra el patrón especificado
- IllegalStateException : cuando este escáner está cerrado
Los siguientes programas ilustran la función anterior:
Programa 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
Programa 2: Para demostrar 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
Programa 3: Para demostrar 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
saltar (patrón de string)
El método skip(String pattern) de la clase java.util.Scanner omite la entrada que coincide con el patrón construido a partir de la string especificada. skip(pattern) y skip(Pattern.compile(pattern)) se comportan exactamente de la misma manera al ser llamados.
Sintaxis:
public Scanner skip(String pattern)
Parámetros: la función acepta un patrón de string de parámetros obligatorio que especifica una string que indica el patrón que se va a omitir
Valor devuelto: la función devuelve este escáner
Excepciones: este método lanza IllegalStateException cuando este escáner está cerrado
Los siguientes programas ilustran la función anterior:
Programa 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
Programa 2: Para demostrar 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
Referencia: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#skip(java.util.regex.Pattern)