Code example for Scanner

Methods: hasNextShort, nextShort, useLocale, useRadix

0
 
        /* 
         * Different locale can only recognize corresponding locale sensitive 
         * string. ',' is used in many locales as group separator. 
         */ 
        s = new Scanner("23,456 23,456");
        s.useLocale(Locale.GERMANY);
        assertFalse(s.hasNextShort());
        try { 
            s.nextShort();
            fail("Should throw InputMismatchException");
        } catch (InputMismatchException e) {
            // Expected 
        } 
        s.useLocale(Locale.ENGLISH);
        // If exception is thrown out, input will not be advanced. 
        assertTrue(s.hasNextShort());
        assertEquals(23456, s.nextShort());
        assertTrue(s.hasNextShort());
        assertEquals(23456, s.nextShort());
 
        /* 
         * ''' is used in many locales as group separator. 
         */ 
        s = new Scanner("23'456 23'456");