Index: src/test/java/tests/api/java/util/ScannerTest.java =================================================================== --- src/test/java/tests/api/java/util/ScannerTest.java (revision 469743) +++ src/test/java/tests/api/java/util/ScannerTest.java (working copy) @@ -3383,6 +3383,29 @@ /** * @throws IOException + * @tests java.util.Scanner#hasNextInt(int) + */ + public void test_hasNextIntI_cache() throws IOException { + s = new Scanner("123 456"); + assertTrue(s.hasNextInt(16)); + assertEquals(291, s.nextInt()); + assertEquals(456, s.nextInt()); + + s = new Scanner("123 456"); + assertTrue(s.hasNextInt(16)); + assertTrue(s.hasNextInt(8)); + assertEquals(83, s.nextInt()); + assertEquals(456, s.nextInt()); + + s = new Scanner("-123 -456 -789"); + assertTrue(s.hasNextInt(8)); + assertEquals(-123, s.nextShort()); + assertEquals(-456, s.nextInt()); + assertTrue(s.hasNextShort(16)); + assertEquals(-789, s.nextInt()); + } + /** + * @throws IOException * @tests java.util.Scanner#hasNextInt() */ public void test_hasNextInt() throws IOException { @@ -4013,9 +4036,33 @@ } /** - * @throws IOException - * @tests java.util.Scanner#hasNextLong(int) - */ + * @throws IOException + * @tests java.util.Scanner#hasNextShort(int) + */ + public void test_hasNextShortI_cache() throws IOException { + s = new Scanner("123 456"); + assertTrue(s.hasNextShort(16)); + assertEquals(291, s.nextShort()); + assertEquals(456, s.nextShort()); + + s = new Scanner("123 456"); + assertTrue(s.hasNextShort(16)); + assertTrue(s.hasNextShort(8)); + assertEquals(83, s.nextShort()); + assertEquals(456, s.nextShort()); + + s = new Scanner("-123 -456 -789"); + assertTrue(s.hasNextShort(8)); + assertEquals(-123, s.nextInt()); + assertEquals(-456, s.nextShort()); + assertTrue(s.hasNextInt(16)); + assertEquals(-789, s.nextShort()); + } + + /** + * @throws IOException + * @tests java.util.Scanner#hasNextLong(int) + */ public void test_hasNextLongI() throws IOException { s = new Scanner("123 456"); assertTrue(s.hasNextLong(10)); Index: src/main/java/java/util/Scanner.java =================================================================== --- src/main/java/java/util/Scanner.java (revision 469743) +++ src/main/java/java/util/Scanner.java (working copy) @@ -137,6 +137,10 @@ // Records whether the underlying readable has more input. private boolean inputExhausted = false; + private Object cacheHasNextValue = null; + + private int cachehasNextIndex = -1; + private enum DataType{ /* * Stands for Integer @@ -571,6 +575,7 @@ boolean hasNext = false; //check whether next token matches the specified pattern if (matcher.matches()) { + cachehasNextIndex = findStartIndex; matchSuccessful = true; hasNext = true; } @@ -805,7 +810,7 @@ String intString = matcher.group(); intString = removeLocaleInfo(intString, DataType.INT); try { - Integer.parseInt(intString, radix); + cacheHasNextValue = Integer.parseInt(intString, radix); isIntValue = true; } catch (NumberFormatException e) { matchSuccessful = false; @@ -927,7 +932,7 @@ String intString = matcher.group(); intString = removeLocaleInfo(intString, DataType.INT); try { - Short.parseShort(intString, radix); + cacheHasNextValue = Short.parseShort(intString, radix); isShortValue = true; } catch (NumberFormatException e) { matchSuccessful = false; @@ -1385,6 +1390,12 @@ * value */ public int nextInt(int radix) { + Object obj = cacheHasNextValue; + cacheHasNextValue = null; + if (obj != null && obj instanceof Integer) { + findStartIndex = cachehasNextIndex; + return (Integer) obj; + } Pattern integerPattern = getIntegerPattern(radix); String intString=next(integerPattern); intString = removeLocaleInfo(intString, DataType.INT); @@ -1566,6 +1577,12 @@ * value, or it is out of range */ public short nextShort(int radix) { + Object obj = cacheHasNextValue; + cacheHasNextValue = null; + if (obj != null && obj instanceof Short) { + findStartIndex = cachehasNextIndex; + return (Short) obj; + } Pattern integerPattern = getIntegerPattern(radix); String intString = next(integerPattern); intString = removeLocaleInfo(intString, DataType.INT);