BAEL-5381: Java Scanner.skip method with examples (#12083)

* BAEL-5381: Java Scanner.skip method with examples

* BAEL-5381: Edit unitTest class name
This commit is contained in:
ACHRAF TAITAI 2022-04-24 14:49:25 +02:00 committed by GitHub
parent c05421898a
commit 0fc7298473
1 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package com.baeldung.scanner;
import org.junit.Test;
import java.util.*;
import java.util.regex.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ScannerUnitTest {
@Test public void scannerSkipUsingPattern() {
String str = "Java scanner skip tutorial";
// 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"));
assertEquals(sc.nextLine(), " scanner skip tutorial");
// Scanner closed
sc.close();
}
@Test public void scannerSkipUsingStringPattern() {
String str = "Java scanner skip tutorial";
// 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");
assertEquals(sc.nextLine(), " scanner skip tutorial");
// Scanner closed
sc.close();
}
}