From 0fc7298473d1e05e891f7dae21852a0b11bcb070 Mon Sep 17 00:00:00 2001 From: ACHRAF TAITAI <43656331+achraftt@users.noreply.github.com> Date: Sun, 24 Apr 2022 14:49:25 +0200 Subject: [PATCH] BAEL-5381: Java Scanner.skip method with examples (#12083) * BAEL-5381: Java Scanner.skip method with examples * BAEL-5381: Edit unitTest class name --- .../com/baeldung/scanner/ScannerUnitTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 core-java-modules/core-java-io-4/src/test/java/com/baeldung/scanner/ScannerUnitTest.java diff --git a/core-java-modules/core-java-io-4/src/test/java/com/baeldung/scanner/ScannerUnitTest.java b/core-java-modules/core-java-io-4/src/test/java/com/baeldung/scanner/ScannerUnitTest.java new file mode 100644 index 0000000000..f11f3d032d --- /dev/null +++ b/core-java-modules/core-java-io-4/src/test/java/com/baeldung/scanner/ScannerUnitTest.java @@ -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(); + } +} \ No newline at end of file