src/java.base/share/classes/java/util/Scanner.java

Print this page




1250      * @param radix The radix to use when scanning numbers
1251      * @return this scanner
1252      * @throws IllegalArgumentException if radix is out of range
1253      */
1254     public Scanner useRadix(int radix) {
1255         if ((radix < Character.MIN_RADIX) || (radix > Character.MAX_RADIX))
1256             throw new IllegalArgumentException("radix:"+radix);
1257 
1258         if (this.defaultRadix == radix)
1259             return this;
1260         modCount++;
1261         this.defaultRadix = radix;
1262         // Force rebuilding and recompilation of radix dependent patterns
1263         integerPattern = null;
1264         return this;
1265     }
1266 
1267     // The next operation should occur in the specified radix but
1268     // the default is left untouched.
1269     private void setRadix(int radix) {



1270         if (this.radix != radix) {
1271             // Force rebuilding and recompilation of radix dependent patterns
1272             integerPattern = null;
1273             this.radix = radix;
1274         }
1275     }
1276 
1277     /**
1278      * Returns the match result of the last scanning operation performed
1279      * by this scanner. This method throws {@code IllegalStateException}
1280      * if no match has been performed, or if the last match was
1281      * not successful.
1282      *
1283      * <p>The various {@code next} methods of {@code Scanner}
1284      * make a match result available if they complete without throwing an
1285      * exception. For instance, after an invocation of the {@link #nextInt}
1286      * method that returned an int, this method returns a
1287      * {@code MatchResult} for the search of the
1288      * <a href="#Integer-regex"><i>Integer</i></a> regular expression
1289      * defined above. Similarly the {@link #findInLine findInLine()},


1794     }
1795 
1796     /**
1797      * Returns true if the next token in this scanner's input can be
1798      * interpreted as a byte value in the default radix using the
1799      * {@link #nextByte} method. The scanner does not advance past any input.
1800      *
1801      * @return true if and only if this scanner's next token is a valid
1802      *         byte value
1803      * @throws IllegalStateException if this scanner is closed
1804      */
1805     public boolean hasNextByte() {
1806         return hasNextByte(defaultRadix);
1807     }
1808 
1809     /**
1810      * Returns true if the next token in this scanner's input can be
1811      * interpreted as a byte value in the specified radix using the
1812      * {@link #nextByte} method. The scanner does not advance past any input.
1813      *




1814      * @param radix the radix used to interpret the token as a byte value
1815      * @return true if and only if this scanner's next token is a valid
1816      *         byte value
1817      * @throws IllegalStateException if this scanner is closed

1818      */
1819     public boolean hasNextByte(int radix) {
1820         setRadix(radix);
1821         boolean result = hasNext(integerPattern());
1822         if (result) { // Cache it
1823             try {
1824                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
1825                     processIntegerToken(hasNextResult) :
1826                     hasNextResult;
1827                 typeCache = Byte.parseByte(s, radix);
1828             } catch (NumberFormatException nfe) {
1829                 result = false;
1830             }
1831         }
1832         return result;
1833     }
1834 
1835     /**
1836      * Scans the next token of the input as a {@code byte}.
1837      *


1852     }
1853 
1854     /**
1855      * Scans the next token of the input as a {@code byte}.
1856      * This method will throw {@code InputMismatchException}
1857      * if the next token cannot be translated into a valid byte value as
1858      * described below. If the translation is successful, the scanner advances
1859      * past the input that matched.
1860      *
1861      * <p> If the next token matches the <a
1862      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
1863      * above then the token is converted into a {@code byte} value as if by
1864      * removing all locale specific prefixes, group separators, and locale
1865      * specific suffixes, then mapping non-ASCII digits into ASCII
1866      * digits via {@link Character#digit Character.digit}, prepending a
1867      * negative sign (-) if the locale specific negative prefixes and suffixes
1868      * were present, and passing the resulting string to
1869      * {@link Byte#parseByte(String, int) Byte.parseByte} with the
1870      * specified radix.
1871      *




1872      * @param radix the radix used to interpret the token as a byte value
1873      * @return the {@code byte} scanned from the input
1874      * @throws InputMismatchException
1875      *         if the next token does not match the <i>Integer</i>
1876      *         regular expression, or is out of range
1877      * @throws NoSuchElementException if input is exhausted
1878      * @throws IllegalStateException if this scanner is closed

1879      */
1880     public byte nextByte(int radix) {
1881         // Check cached result
1882         if ((typeCache != null) && (typeCache instanceof Byte)
1883             && this.radix == radix) {
1884             byte val = ((Byte)typeCache).byteValue();
1885             useTypeCache();
1886             return val;
1887         }
1888         setRadix(radix);
1889         clearCaches();
1890         // Search for next byte
1891         try {
1892             String s = next(integerPattern());
1893             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
1894                 s = processIntegerToken(s);
1895             return Byte.parseByte(s, radix);
1896         } catch (NumberFormatException nfe) {
1897             position = matcher.start(); // don't skip bad token
1898             throw new InputMismatchException(nfe.getMessage());


1900     }
1901 
1902     /**
1903      * Returns true if the next token in this scanner's input can be
1904      * interpreted as a short value in the default radix using the
1905      * {@link #nextShort} method. The scanner does not advance past any input.
1906      *
1907      * @return true if and only if this scanner's next token is a valid
1908      *         short value in the default radix
1909      * @throws IllegalStateException if this scanner is closed
1910      */
1911     public boolean hasNextShort() {
1912         return hasNextShort(defaultRadix);
1913     }
1914 
1915     /**
1916      * Returns true if the next token in this scanner's input can be
1917      * interpreted as a short value in the specified radix using the
1918      * {@link #nextShort} method. The scanner does not advance past any input.
1919      *




1920      * @param radix the radix used to interpret the token as a short value
1921      * @return true if and only if this scanner's next token is a valid
1922      *         short value in the specified radix
1923      * @throws IllegalStateException if this scanner is closed

1924      */
1925     public boolean hasNextShort(int radix) {
1926         setRadix(radix);
1927         boolean result = hasNext(integerPattern());
1928         if (result) { // Cache it
1929             try {
1930                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
1931                     processIntegerToken(hasNextResult) :
1932                     hasNextResult;
1933                 typeCache = Short.parseShort(s, radix);
1934             } catch (NumberFormatException nfe) {
1935                 result = false;
1936             }
1937         }
1938         return result;
1939     }
1940 
1941     /**
1942      * Scans the next token of the input as a {@code short}.
1943      *


1958     }
1959 
1960     /**
1961      * Scans the next token of the input as a {@code short}.
1962      * This method will throw {@code InputMismatchException}
1963      * if the next token cannot be translated into a valid short value as
1964      * described below. If the translation is successful, the scanner advances
1965      * past the input that matched.
1966      *
1967      * <p> If the next token matches the <a
1968      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
1969      * above then the token is converted into a {@code short} value as if by
1970      * removing all locale specific prefixes, group separators, and locale
1971      * specific suffixes, then mapping non-ASCII digits into ASCII
1972      * digits via {@link Character#digit Character.digit}, prepending a
1973      * negative sign (-) if the locale specific negative prefixes and suffixes
1974      * were present, and passing the resulting string to
1975      * {@link Short#parseShort(String, int) Short.parseShort} with the
1976      * specified radix.
1977      *




1978      * @param radix the radix used to interpret the token as a short value
1979      * @return the {@code short} scanned from the input
1980      * @throws InputMismatchException
1981      *         if the next token does not match the <i>Integer</i>
1982      *         regular expression, or is out of range
1983      * @throws NoSuchElementException if input is exhausted
1984      * @throws IllegalStateException if this scanner is closed

1985      */
1986     public short nextShort(int radix) {
1987         // Check cached result
1988         if ((typeCache != null) && (typeCache instanceof Short)
1989             && this.radix == radix) {
1990             short val = ((Short)typeCache).shortValue();
1991             useTypeCache();
1992             return val;
1993         }
1994         setRadix(radix);
1995         clearCaches();
1996         // Search for next short
1997         try {
1998             String s = next(integerPattern());
1999             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
2000                 s = processIntegerToken(s);
2001             return Short.parseShort(s, radix);
2002         } catch (NumberFormatException nfe) {
2003             position = matcher.start(); // don't skip bad token
2004             throw new InputMismatchException(nfe.getMessage());


2006     }
2007 
2008     /**
2009      * Returns true if the next token in this scanner's input can be
2010      * interpreted as an int value in the default radix using the
2011      * {@link #nextInt} method. The scanner does not advance past any input.
2012      *
2013      * @return true if and only if this scanner's next token is a valid
2014      *         int value
2015      * @throws IllegalStateException if this scanner is closed
2016      */
2017     public boolean hasNextInt() {
2018         return hasNextInt(defaultRadix);
2019     }
2020 
2021     /**
2022      * Returns true if the next token in this scanner's input can be
2023      * interpreted as an int value in the specified radix using the
2024      * {@link #nextInt} method. The scanner does not advance past any input.
2025      *




2026      * @param radix the radix used to interpret the token as an int value
2027      * @return true if and only if this scanner's next token is a valid
2028      *         int value
2029      * @throws IllegalStateException if this scanner is closed

2030      */
2031     public boolean hasNextInt(int radix) {
2032         setRadix(radix);
2033         boolean result = hasNext(integerPattern());
2034         if (result) { // Cache it
2035             try {
2036                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
2037                     processIntegerToken(hasNextResult) :
2038                     hasNextResult;
2039                 typeCache = Integer.parseInt(s, radix);
2040             } catch (NumberFormatException nfe) {
2041                 result = false;
2042             }
2043         }
2044         return result;
2045     }
2046 
2047     /**
2048      * The integer token must be stripped of prefixes, group separators,
2049      * and suffixes, non ascii digits must be converted into ascii digits


2088     }
2089 
2090     /**
2091      * Scans the next token of the input as an {@code int}.
2092      * This method will throw {@code InputMismatchException}
2093      * if the next token cannot be translated into a valid int value as
2094      * described below. If the translation is successful, the scanner advances
2095      * past the input that matched.
2096      *
2097      * <p> If the next token matches the <a
2098      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
2099      * above then the token is converted into an {@code int} value as if by
2100      * removing all locale specific prefixes, group separators, and locale
2101      * specific suffixes, then mapping non-ASCII digits into ASCII
2102      * digits via {@link Character#digit Character.digit}, prepending a
2103      * negative sign (-) if the locale specific negative prefixes and suffixes
2104      * were present, and passing the resulting string to
2105      * {@link Integer#parseInt(String, int) Integer.parseInt} with the
2106      * specified radix.
2107      *




2108      * @param radix the radix used to interpret the token as an int value
2109      * @return the {@code int} scanned from the input
2110      * @throws InputMismatchException
2111      *         if the next token does not match the <i>Integer</i>
2112      *         regular expression, or is out of range
2113      * @throws NoSuchElementException if input is exhausted
2114      * @throws IllegalStateException if this scanner is closed

2115      */
2116     public int nextInt(int radix) {
2117         // Check cached result
2118         if ((typeCache != null) && (typeCache instanceof Integer)
2119             && this.radix == radix) {
2120             int val = ((Integer)typeCache).intValue();
2121             useTypeCache();
2122             return val;
2123         }
2124         setRadix(radix);
2125         clearCaches();
2126         // Search for next int
2127         try {
2128             String s = next(integerPattern());
2129             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
2130                 s = processIntegerToken(s);
2131             return Integer.parseInt(s, radix);
2132         } catch (NumberFormatException nfe) {
2133             position = matcher.start(); // don't skip bad token
2134             throw new InputMismatchException(nfe.getMessage());


2136     }
2137 
2138     /**
2139      * Returns true if the next token in this scanner's input can be
2140      * interpreted as a long value in the default radix using the
2141      * {@link #nextLong} method. The scanner does not advance past any input.
2142      *
2143      * @return true if and only if this scanner's next token is a valid
2144      *         long value
2145      * @throws IllegalStateException if this scanner is closed
2146      */
2147     public boolean hasNextLong() {
2148         return hasNextLong(defaultRadix);
2149     }
2150 
2151     /**
2152      * Returns true if the next token in this scanner's input can be
2153      * interpreted as a long value in the specified radix using the
2154      * {@link #nextLong} method. The scanner does not advance past any input.
2155      *




2156      * @param radix the radix used to interpret the token as a long value
2157      * @return true if and only if this scanner's next token is a valid
2158      *         long value
2159      * @throws IllegalStateException if this scanner is closed

2160      */
2161     public boolean hasNextLong(int radix) {
2162         setRadix(radix);
2163         boolean result = hasNext(integerPattern());
2164         if (result) { // Cache it
2165             try {
2166                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
2167                     processIntegerToken(hasNextResult) :
2168                     hasNextResult;
2169                 typeCache = Long.parseLong(s, radix);
2170             } catch (NumberFormatException nfe) {
2171                 result = false;
2172             }
2173         }
2174         return result;
2175     }
2176 
2177     /**
2178      * Scans the next token of the input as a {@code long}.
2179      *


2194     }
2195 
2196     /**
2197      * Scans the next token of the input as a {@code long}.
2198      * This method will throw {@code InputMismatchException}
2199      * if the next token cannot be translated into a valid long value as
2200      * described below. If the translation is successful, the scanner advances
2201      * past the input that matched.
2202      *
2203      * <p> If the next token matches the <a
2204      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
2205      * above then the token is converted into a {@code long} value as if by
2206      * removing all locale specific prefixes, group separators, and locale
2207      * specific suffixes, then mapping non-ASCII digits into ASCII
2208      * digits via {@link Character#digit Character.digit}, prepending a
2209      * negative sign (-) if the locale specific negative prefixes and suffixes
2210      * were present, and passing the resulting string to
2211      * {@link Long#parseLong(String, int) Long.parseLong} with the
2212      * specified radix.
2213      *




2214      * @param radix the radix used to interpret the token as an int value
2215      * @return the {@code long} scanned from the input
2216      * @throws InputMismatchException
2217      *         if the next token does not match the <i>Integer</i>
2218      *         regular expression, or is out of range
2219      * @throws NoSuchElementException if input is exhausted
2220      * @throws IllegalStateException if this scanner is closed

2221      */
2222     public long nextLong(int radix) {
2223         // Check cached result
2224         if ((typeCache != null) && (typeCache instanceof Long)
2225             && this.radix == radix) {
2226             long val = ((Long)typeCache).longValue();
2227             useTypeCache();
2228             return val;
2229         }
2230         setRadix(radix);
2231         clearCaches();
2232         try {
2233             String s = next(integerPattern());
2234             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
2235                 s = processIntegerToken(s);
2236             return Long.parseLong(s, radix);
2237         } catch (NumberFormatException nfe) {
2238             position = matcher.start(); // don't skip bad token
2239             throw new InputMismatchException(nfe.getMessage());
2240         }


2433     /**
2434      * Returns true if the next token in this scanner's input can be
2435      * interpreted as a {@code BigInteger} in the default radix using the
2436      * {@link #nextBigInteger} method. The scanner does not advance past any
2437      * input.
2438      *
2439      * @return true if and only if this scanner's next token is a valid
2440      *         {@code BigInteger}
2441      * @throws IllegalStateException if this scanner is closed
2442      */
2443     public boolean hasNextBigInteger() {
2444         return hasNextBigInteger(defaultRadix);
2445     }
2446 
2447     /**
2448      * Returns true if the next token in this scanner's input can be
2449      * interpreted as a {@code BigInteger} in the specified radix using
2450      * the {@link #nextBigInteger} method. The scanner does not advance past
2451      * any input.
2452      *




2453      * @param radix the radix used to interpret the token as an integer
2454      * @return true if and only if this scanner's next token is a valid
2455      *         {@code BigInteger}
2456      * @throws IllegalStateException if this scanner is closed

2457      */
2458     public boolean hasNextBigInteger(int radix) {
2459         setRadix(radix);
2460         boolean result = hasNext(integerPattern());
2461         if (result) { // Cache it
2462             try {
2463                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
2464                     processIntegerToken(hasNextResult) :
2465                     hasNextResult;
2466                 typeCache = new BigInteger(s, radix);
2467             } catch (NumberFormatException nfe) {
2468                 result = false;
2469             }
2470         }
2471         return result;
2472     }
2473 
2474     /**
2475      * Scans the next token of the input as a {@link java.math.BigInteger
2476      * BigInteger}.


2487      * @throws NoSuchElementException if the input is exhausted
2488      * @throws IllegalStateException if this scanner is closed
2489      */
2490     public BigInteger nextBigInteger() {
2491         return nextBigInteger(defaultRadix);
2492     }
2493 
2494     /**
2495      * Scans the next token of the input as a {@link java.math.BigInteger
2496      * BigInteger}.
2497      *
2498      * <p> If the next token matches the <a
2499      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
2500      * above then the token is converted into a {@code BigInteger} value as if
2501      * by removing all group separators, mapping non-ASCII digits into ASCII
2502      * digits via the {@link Character#digit Character.digit}, and passing the
2503      * resulting string to the {@link
2504      * java.math.BigInteger#BigInteger(java.lang.String)
2505      * BigInteger(String, int)} constructor with the specified radix.
2506      *




2507      * @param radix the radix used to interpret the token
2508      * @return the {@code BigInteger} scanned from the input
2509      * @throws InputMismatchException
2510      *         if the next token does not match the <i>Integer</i>
2511      *         regular expression, or is out of range
2512      * @throws NoSuchElementException if the input is exhausted
2513      * @throws IllegalStateException if this scanner is closed

2514      */
2515     public BigInteger nextBigInteger(int radix) {
2516         // Check cached result
2517         if ((typeCache != null) && (typeCache instanceof BigInteger)
2518             && this.radix == radix) {
2519             BigInteger val = (BigInteger)typeCache;
2520             useTypeCache();
2521             return val;
2522         }
2523         setRadix(radix);
2524         clearCaches();
2525         // Search for next int
2526         try {
2527             String s = next(integerPattern());
2528             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
2529                 s = processIntegerToken(s);
2530             return new BigInteger(s, radix);
2531         } catch (NumberFormatException nfe) {
2532             position = matcher.start(); // don't skip bad token
2533             throw new InputMismatchException(nfe.getMessage());




1250      * @param radix The radix to use when scanning numbers
1251      * @return this scanner
1252      * @throws IllegalArgumentException if radix is out of range
1253      */
1254     public Scanner useRadix(int radix) {
1255         if ((radix < Character.MIN_RADIX) || (radix > Character.MAX_RADIX))
1256             throw new IllegalArgumentException("radix:"+radix);
1257 
1258         if (this.defaultRadix == radix)
1259             return this;
1260         modCount++;
1261         this.defaultRadix = radix;
1262         // Force rebuilding and recompilation of radix dependent patterns
1263         integerPattern = null;
1264         return this;
1265     }
1266 
1267     // The next operation should occur in the specified radix but
1268     // the default is left untouched.
1269     private void setRadix(int radix) {
1270         if ((radix < Character.MIN_RADIX) || (radix > Character.MAX_RADIX))
1271             throw new IllegalArgumentException("radix:"+radix);
1272 
1273         if (this.radix != radix) {
1274             // Force rebuilding and recompilation of radix dependent patterns
1275             integerPattern = null;
1276             this.radix = radix;
1277         }
1278     }
1279 
1280     /**
1281      * Returns the match result of the last scanning operation performed
1282      * by this scanner. This method throws {@code IllegalStateException}
1283      * if no match has been performed, or if the last match was
1284      * not successful.
1285      *
1286      * <p>The various {@code next} methods of {@code Scanner}
1287      * make a match result available if they complete without throwing an
1288      * exception. For instance, after an invocation of the {@link #nextInt}
1289      * method that returned an int, this method returns a
1290      * {@code MatchResult} for the search of the
1291      * <a href="#Integer-regex"><i>Integer</i></a> regular expression
1292      * defined above. Similarly the {@link #findInLine findInLine()},


1797     }
1798 
1799     /**
1800      * Returns true if the next token in this scanner's input can be
1801      * interpreted as a byte value in the default radix using the
1802      * {@link #nextByte} method. The scanner does not advance past any input.
1803      *
1804      * @return true if and only if this scanner's next token is a valid
1805      *         byte value
1806      * @throws IllegalStateException if this scanner is closed
1807      */
1808     public boolean hasNextByte() {
1809         return hasNextByte(defaultRadix);
1810     }
1811 
1812     /**
1813      * Returns true if the next token in this scanner's input can be
1814      * interpreted as a byte value in the specified radix using the
1815      * {@link #nextByte} method. The scanner does not advance past any input.
1816      *
1817      * <p>If the radix is less than {@link Character#MIN_RADIX Character.MIN_RADIX}
1818      * or greater than {@link Character#MAX_RADIX Character.MAX_RADIX}, then an
1819      * {@code IllegalArgumentException} is thrown.
1820      *
1821      * @param radix the radix used to interpret the token as a byte value
1822      * @return true if and only if this scanner's next token is a valid
1823      *         byte value
1824      * @throws IllegalStateException if this scanner is closed
1825      * @throws IllegalArgumentException if the radix is out of range
1826      */
1827     public boolean hasNextByte(int radix) {
1828         setRadix(radix);
1829         boolean result = hasNext(integerPattern());
1830         if (result) { // Cache it
1831             try {
1832                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
1833                     processIntegerToken(hasNextResult) :
1834                     hasNextResult;
1835                 typeCache = Byte.parseByte(s, radix);
1836             } catch (NumberFormatException nfe) {
1837                 result = false;
1838             }
1839         }
1840         return result;
1841     }
1842 
1843     /**
1844      * Scans the next token of the input as a {@code byte}.
1845      *


1860     }
1861 
1862     /**
1863      * Scans the next token of the input as a {@code byte}.
1864      * This method will throw {@code InputMismatchException}
1865      * if the next token cannot be translated into a valid byte value as
1866      * described below. If the translation is successful, the scanner advances
1867      * past the input that matched.
1868      *
1869      * <p> If the next token matches the <a
1870      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
1871      * above then the token is converted into a {@code byte} value as if by
1872      * removing all locale specific prefixes, group separators, and locale
1873      * specific suffixes, then mapping non-ASCII digits into ASCII
1874      * digits via {@link Character#digit Character.digit}, prepending a
1875      * negative sign (-) if the locale specific negative prefixes and suffixes
1876      * were present, and passing the resulting string to
1877      * {@link Byte#parseByte(String, int) Byte.parseByte} with the
1878      * specified radix.
1879      *
1880      * <p>If the radix is less than {@link Character#MIN_RADIX Character.MIN_RADIX}
1881      * or greater than {@link Character#MAX_RADIX Character.MAX_RADIX}, then an
1882      * {@code IllegalArgumentException} is thrown.
1883      *
1884      * @param radix the radix used to interpret the token as a byte value
1885      * @return the {@code byte} scanned from the input
1886      * @throws InputMismatchException
1887      *         if the next token does not match the <i>Integer</i>
1888      *         regular expression, or is out of range
1889      * @throws NoSuchElementException if input is exhausted
1890      * @throws IllegalStateException if this scanner is closed
1891      * @throws IllegalArgumentException if the radix is out of range
1892      */
1893     public byte nextByte(int radix) {
1894         // Check cached result
1895         if ((typeCache != null) && (typeCache instanceof Byte)
1896             && this.radix == radix) {
1897             byte val = ((Byte)typeCache).byteValue();
1898             useTypeCache();
1899             return val;
1900         }
1901         setRadix(radix);
1902         clearCaches();
1903         // Search for next byte
1904         try {
1905             String s = next(integerPattern());
1906             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
1907                 s = processIntegerToken(s);
1908             return Byte.parseByte(s, radix);
1909         } catch (NumberFormatException nfe) {
1910             position = matcher.start(); // don't skip bad token
1911             throw new InputMismatchException(nfe.getMessage());


1913     }
1914 
1915     /**
1916      * Returns true if the next token in this scanner's input can be
1917      * interpreted as a short value in the default radix using the
1918      * {@link #nextShort} method. The scanner does not advance past any input.
1919      *
1920      * @return true if and only if this scanner's next token is a valid
1921      *         short value in the default radix
1922      * @throws IllegalStateException if this scanner is closed
1923      */
1924     public boolean hasNextShort() {
1925         return hasNextShort(defaultRadix);
1926     }
1927 
1928     /**
1929      * Returns true if the next token in this scanner's input can be
1930      * interpreted as a short value in the specified radix using the
1931      * {@link #nextShort} method. The scanner does not advance past any input.
1932      *
1933      * <p>If the radix is less than {@link Character#MIN_RADIX Character.MIN_RADIX}
1934      * or greater than {@link Character#MAX_RADIX Character.MAX_RADIX}, then an
1935      * {@code IllegalArgumentException} is thrown.
1936      *
1937      * @param radix the radix used to interpret the token as a short value
1938      * @return true if and only if this scanner's next token is a valid
1939      *         short value in the specified radix
1940      * @throws IllegalStateException if this scanner is closed
1941      * @throws IllegalArgumentException if the radix is out of range
1942      */
1943     public boolean hasNextShort(int radix) {
1944         setRadix(radix);
1945         boolean result = hasNext(integerPattern());
1946         if (result) { // Cache it
1947             try {
1948                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
1949                     processIntegerToken(hasNextResult) :
1950                     hasNextResult;
1951                 typeCache = Short.parseShort(s, radix);
1952             } catch (NumberFormatException nfe) {
1953                 result = false;
1954             }
1955         }
1956         return result;
1957     }
1958 
1959     /**
1960      * Scans the next token of the input as a {@code short}.
1961      *


1976     }
1977 
1978     /**
1979      * Scans the next token of the input as a {@code short}.
1980      * This method will throw {@code InputMismatchException}
1981      * if the next token cannot be translated into a valid short value as
1982      * described below. If the translation is successful, the scanner advances
1983      * past the input that matched.
1984      *
1985      * <p> If the next token matches the <a
1986      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
1987      * above then the token is converted into a {@code short} value as if by
1988      * removing all locale specific prefixes, group separators, and locale
1989      * specific suffixes, then mapping non-ASCII digits into ASCII
1990      * digits via {@link Character#digit Character.digit}, prepending a
1991      * negative sign (-) if the locale specific negative prefixes and suffixes
1992      * were present, and passing the resulting string to
1993      * {@link Short#parseShort(String, int) Short.parseShort} with the
1994      * specified radix.
1995      *
1996      * <p>If the radix is less than {@link Character#MIN_RADIX Character.MIN_RADIX}
1997      * or greater than {@link Character#MAX_RADIX Character.MAX_RADIX}, then an
1998      * {@code IllegalArgumentException} is thrown.
1999      *
2000      * @param radix the radix used to interpret the token as a short value
2001      * @return the {@code short} scanned from the input
2002      * @throws InputMismatchException
2003      *         if the next token does not match the <i>Integer</i>
2004      *         regular expression, or is out of range
2005      * @throws NoSuchElementException if input is exhausted
2006      * @throws IllegalStateException if this scanner is closed
2007      * @throws IllegalArgumentException if the radix is out of range
2008      */
2009     public short nextShort(int radix) {
2010         // Check cached result
2011         if ((typeCache != null) && (typeCache instanceof Short)
2012             && this.radix == radix) {
2013             short val = ((Short)typeCache).shortValue();
2014             useTypeCache();
2015             return val;
2016         }
2017         setRadix(radix);
2018         clearCaches();
2019         // Search for next short
2020         try {
2021             String s = next(integerPattern());
2022             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
2023                 s = processIntegerToken(s);
2024             return Short.parseShort(s, radix);
2025         } catch (NumberFormatException nfe) {
2026             position = matcher.start(); // don't skip bad token
2027             throw new InputMismatchException(nfe.getMessage());


2029     }
2030 
2031     /**
2032      * Returns true if the next token in this scanner's input can be
2033      * interpreted as an int value in the default radix using the
2034      * {@link #nextInt} method. The scanner does not advance past any input.
2035      *
2036      * @return true if and only if this scanner's next token is a valid
2037      *         int value
2038      * @throws IllegalStateException if this scanner is closed
2039      */
2040     public boolean hasNextInt() {
2041         return hasNextInt(defaultRadix);
2042     }
2043 
2044     /**
2045      * Returns true if the next token in this scanner's input can be
2046      * interpreted as an int value in the specified radix using the
2047      * {@link #nextInt} method. The scanner does not advance past any input.
2048      *
2049      * <p>If the radix is less than {@link Character#MIN_RADIX Character.MIN_RADIX}
2050      * or greater than {@link Character#MAX_RADIX Character.MAX_RADIX}, then an
2051      * {@code IllegalArgumentException} is thrown.
2052      *
2053      * @param radix the radix used to interpret the token as an int value
2054      * @return true if and only if this scanner's next token is a valid
2055      *         int value
2056      * @throws IllegalStateException if this scanner is closed
2057      * @throws IllegalArgumentException if the radix is out of range
2058      */
2059     public boolean hasNextInt(int radix) {
2060         setRadix(radix);
2061         boolean result = hasNext(integerPattern());
2062         if (result) { // Cache it
2063             try {
2064                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
2065                     processIntegerToken(hasNextResult) :
2066                     hasNextResult;
2067                 typeCache = Integer.parseInt(s, radix);
2068             } catch (NumberFormatException nfe) {
2069                 result = false;
2070             }
2071         }
2072         return result;
2073     }
2074 
2075     /**
2076      * The integer token must be stripped of prefixes, group separators,
2077      * and suffixes, non ascii digits must be converted into ascii digits


2116     }
2117 
2118     /**
2119      * Scans the next token of the input as an {@code int}.
2120      * This method will throw {@code InputMismatchException}
2121      * if the next token cannot be translated into a valid int value as
2122      * described below. If the translation is successful, the scanner advances
2123      * past the input that matched.
2124      *
2125      * <p> If the next token matches the <a
2126      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
2127      * above then the token is converted into an {@code int} value as if by
2128      * removing all locale specific prefixes, group separators, and locale
2129      * specific suffixes, then mapping non-ASCII digits into ASCII
2130      * digits via {@link Character#digit Character.digit}, prepending a
2131      * negative sign (-) if the locale specific negative prefixes and suffixes
2132      * were present, and passing the resulting string to
2133      * {@link Integer#parseInt(String, int) Integer.parseInt} with the
2134      * specified radix.
2135      *
2136      * <p>If the radix is less than {@link Character#MIN_RADIX Character.MIN_RADIX}
2137      * or greater than {@link Character#MAX_RADIX Character.MAX_RADIX}, then an
2138      * {@code IllegalArgumentException} is thrown.
2139      *
2140      * @param radix the radix used to interpret the token as an int value
2141      * @return the {@code int} scanned from the input
2142      * @throws InputMismatchException
2143      *         if the next token does not match the <i>Integer</i>
2144      *         regular expression, or is out of range
2145      * @throws NoSuchElementException if input is exhausted
2146      * @throws IllegalStateException if this scanner is closed
2147      * @throws IllegalArgumentException if the radix is out of range
2148      */
2149     public int nextInt(int radix) {
2150         // Check cached result
2151         if ((typeCache != null) && (typeCache instanceof Integer)
2152             && this.radix == radix) {
2153             int val = ((Integer)typeCache).intValue();
2154             useTypeCache();
2155             return val;
2156         }
2157         setRadix(radix);
2158         clearCaches();
2159         // Search for next int
2160         try {
2161             String s = next(integerPattern());
2162             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
2163                 s = processIntegerToken(s);
2164             return Integer.parseInt(s, radix);
2165         } catch (NumberFormatException nfe) {
2166             position = matcher.start(); // don't skip bad token
2167             throw new InputMismatchException(nfe.getMessage());


2169     }
2170 
2171     /**
2172      * Returns true if the next token in this scanner's input can be
2173      * interpreted as a long value in the default radix using the
2174      * {@link #nextLong} method. The scanner does not advance past any input.
2175      *
2176      * @return true if and only if this scanner's next token is a valid
2177      *         long value
2178      * @throws IllegalStateException if this scanner is closed
2179      */
2180     public boolean hasNextLong() {
2181         return hasNextLong(defaultRadix);
2182     }
2183 
2184     /**
2185      * Returns true if the next token in this scanner's input can be
2186      * interpreted as a long value in the specified radix using the
2187      * {@link #nextLong} method. The scanner does not advance past any input.
2188      *
2189      * <p>If the radix is less than {@link Character#MIN_RADIX Character.MIN_RADIX}
2190      * or greater than {@link Character#MAX_RADIX Character.MAX_RADIX}, then an
2191      * {@code IllegalArgumentException} is thrown.
2192      *
2193      * @param radix the radix used to interpret the token as a long value
2194      * @return true if and only if this scanner's next token is a valid
2195      *         long value
2196      * @throws IllegalStateException if this scanner is closed
2197      * @throws IllegalArgumentException if the radix is out of range
2198      */
2199     public boolean hasNextLong(int radix) {
2200         setRadix(radix);
2201         boolean result = hasNext(integerPattern());
2202         if (result) { // Cache it
2203             try {
2204                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
2205                     processIntegerToken(hasNextResult) :
2206                     hasNextResult;
2207                 typeCache = Long.parseLong(s, radix);
2208             } catch (NumberFormatException nfe) {
2209                 result = false;
2210             }
2211         }
2212         return result;
2213     }
2214 
2215     /**
2216      * Scans the next token of the input as a {@code long}.
2217      *


2232     }
2233 
2234     /**
2235      * Scans the next token of the input as a {@code long}.
2236      * This method will throw {@code InputMismatchException}
2237      * if the next token cannot be translated into a valid long value as
2238      * described below. If the translation is successful, the scanner advances
2239      * past the input that matched.
2240      *
2241      * <p> If the next token matches the <a
2242      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
2243      * above then the token is converted into a {@code long} value as if by
2244      * removing all locale specific prefixes, group separators, and locale
2245      * specific suffixes, then mapping non-ASCII digits into ASCII
2246      * digits via {@link Character#digit Character.digit}, prepending a
2247      * negative sign (-) if the locale specific negative prefixes and suffixes
2248      * were present, and passing the resulting string to
2249      * {@link Long#parseLong(String, int) Long.parseLong} with the
2250      * specified radix.
2251      *
2252      * <p>If the radix is less than {@link Character#MIN_RADIX Character.MIN_RADIX}
2253      * or greater than {@link Character#MAX_RADIX Character.MAX_RADIX}, then an
2254      * {@code IllegalArgumentException} is thrown.
2255      *
2256      * @param radix the radix used to interpret the token as an int value
2257      * @return the {@code long} scanned from the input
2258      * @throws InputMismatchException
2259      *         if the next token does not match the <i>Integer</i>
2260      *         regular expression, or is out of range
2261      * @throws NoSuchElementException if input is exhausted
2262      * @throws IllegalStateException if this scanner is closed
2263      * @throws IllegalArgumentException if the radix is out of range
2264      */
2265     public long nextLong(int radix) {
2266         // Check cached result
2267         if ((typeCache != null) && (typeCache instanceof Long)
2268             && this.radix == radix) {
2269             long val = ((Long)typeCache).longValue();
2270             useTypeCache();
2271             return val;
2272         }
2273         setRadix(radix);
2274         clearCaches();
2275         try {
2276             String s = next(integerPattern());
2277             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
2278                 s = processIntegerToken(s);
2279             return Long.parseLong(s, radix);
2280         } catch (NumberFormatException nfe) {
2281             position = matcher.start(); // don't skip bad token
2282             throw new InputMismatchException(nfe.getMessage());
2283         }


2476     /**
2477      * Returns true if the next token in this scanner's input can be
2478      * interpreted as a {@code BigInteger} in the default radix using the
2479      * {@link #nextBigInteger} method. The scanner does not advance past any
2480      * input.
2481      *
2482      * @return true if and only if this scanner's next token is a valid
2483      *         {@code BigInteger}
2484      * @throws IllegalStateException if this scanner is closed
2485      */
2486     public boolean hasNextBigInteger() {
2487         return hasNextBigInteger(defaultRadix);
2488     }
2489 
2490     /**
2491      * Returns true if the next token in this scanner's input can be
2492      * interpreted as a {@code BigInteger} in the specified radix using
2493      * the {@link #nextBigInteger} method. The scanner does not advance past
2494      * any input.
2495      *
2496      * <p>If the radix is less than {@link Character#MIN_RADIX Character.MIN_RADIX}
2497      * or greater than {@link Character#MAX_RADIX Character.MAX_RADIX}, then an
2498      * {@code IllegalArgumentException} is thrown.
2499      *
2500      * @param radix the radix used to interpret the token as an integer
2501      * @return true if and only if this scanner's next token is a valid
2502      *         {@code BigInteger}
2503      * @throws IllegalStateException if this scanner is closed
2504      * @throws IllegalArgumentException if the radix is out of range
2505      */
2506     public boolean hasNextBigInteger(int radix) {
2507         setRadix(radix);
2508         boolean result = hasNext(integerPattern());
2509         if (result) { // Cache it
2510             try {
2511                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
2512                     processIntegerToken(hasNextResult) :
2513                     hasNextResult;
2514                 typeCache = new BigInteger(s, radix);
2515             } catch (NumberFormatException nfe) {
2516                 result = false;
2517             }
2518         }
2519         return result;
2520     }
2521 
2522     /**
2523      * Scans the next token of the input as a {@link java.math.BigInteger
2524      * BigInteger}.


2535      * @throws NoSuchElementException if the input is exhausted
2536      * @throws IllegalStateException if this scanner is closed
2537      */
2538     public BigInteger nextBigInteger() {
2539         return nextBigInteger(defaultRadix);
2540     }
2541 
2542     /**
2543      * Scans the next token of the input as a {@link java.math.BigInteger
2544      * BigInteger}.
2545      *
2546      * <p> If the next token matches the <a
2547      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
2548      * above then the token is converted into a {@code BigInteger} value as if
2549      * by removing all group separators, mapping non-ASCII digits into ASCII
2550      * digits via the {@link Character#digit Character.digit}, and passing the
2551      * resulting string to the {@link
2552      * java.math.BigInteger#BigInteger(java.lang.String)
2553      * BigInteger(String, int)} constructor with the specified radix.
2554      *
2555      * <p>If the radix is less than {@link Character#MIN_RADIX Character.MIN_RADIX}
2556      * or greater than {@link Character#MAX_RADIX Character.MAX_RADIX}, then an
2557      * {@code IllegalArgumentException} is thrown.
2558      *
2559      * @param radix the radix used to interpret the token
2560      * @return the {@code BigInteger} scanned from the input
2561      * @throws InputMismatchException
2562      *         if the next token does not match the <i>Integer</i>
2563      *         regular expression, or is out of range
2564      * @throws NoSuchElementException if the input is exhausted
2565      * @throws IllegalStateException if this scanner is closed
2566      * @throws IllegalArgumentException if the radix is out of range
2567      */
2568     public BigInteger nextBigInteger(int radix) {
2569         // Check cached result
2570         if ((typeCache != null) && (typeCache instanceof BigInteger)
2571             && this.radix == radix) {
2572             BigInteger val = (BigInteger)typeCache;
2573             useTypeCache();
2574             return val;
2575         }
2576         setRadix(radix);
2577         clearCaches();
2578         // Search for next int
2579         try {
2580             String s = next(integerPattern());
2581             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
2582                 s = processIntegerToken(s);
2583             return new BigInteger(s, radix);
2584         } catch (NumberFormatException nfe) {
2585             position = matcher.start(); // don't skip bad token
2586             throw new InputMismatchException(nfe.getMessage());