Code example for Scanner

Methods: close, match, skip

0
            fail("Should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
 
        // If pattern does not match, NoSuchElementException will be thrown out. 
        s = new Scanner("1234");
        try { 
            s.skip(Pattern.compile("\\p{Lower}"));
            fail("Should throw NoSuchElementException");
        } catch (NoSuchElementException e) {
            // expected 
        } 
        // Then, no matchResult will be thrown out. 
        try { 
            s.match();
            fail("Should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // expected 
        } 
 
        s.skip(Pattern.compile("\\p{Digit}"));
        MatchResult matchResult = s.match();
        assertEquals(0, matchResult.start());
        assertEquals(1, matchResult.end());
 
        s.skip(Pattern.compile("\\p{Digit}+"));
        matchResult = s.match();
        assertEquals(1, matchResult.start());
        assertEquals(4, matchResult.end());
 
        s.close();
        try { 
            s.skip(Pattern.compile("test"));
            fail("Should throw IllegalStateException");
        } catch (IllegalStateException e) {
            // expected 
        }