You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by gh...@apache.org on 2006/07/14 13:49:01 UTC
svn commit: r421880 - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/util/Scanner.java
test/java/tests/api/java/util/ScannerTest.java
Author: gharley
Date: Fri Jul 14 04:49:00 2006
New Revision: 421880
URL: http://svn.apache.org/viewvc?rev=421880&view=rev
Log:
HARMONY 874 : [luni] Implementation of some nextXXX methods for java.util.Scanner
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java?rev=421880&r1=421879&r2=421880&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java Fri Jul 14 04:49:00 2006
@@ -473,7 +473,7 @@
* matched the pattern.
*
* The invocation of this method in the form hasNext(pattern) behaves in the
- * same way as the invocaiton of hasNext(Pattern.compile(pattern)).
+ * same way as the invocation of hasNext(Pattern.compile(pattern)).
*
* @param pattern
* the string specifying the pattern to scan for
@@ -491,14 +491,46 @@
throw new NotYetImplementedException();
}
- //TODO: To implement this feature
+ /**
+ * Returns true if this scanner's next token can be translated into a valid
+ * BigInteger in the default radix. The scanner does not advance past the
+ * input.
+ *
+ * @return true iff the next token in this scanner's input can be translated
+ * into a valid BigInteger
+ * @throws IllegalStateException
+ * if the scanner has been closed
+ */
public boolean hasNextBigInteger() {
- throw new NotYetImplementedException();
+ return hasNextBigInteger(integerRadix);
}
- //TODO: To implement this feature
+ /**
+ * Returns true if this scanner's next token can be translated into a valid
+ * BigInteger in the specified radix. The scanner does not advance past the
+ * input.
+ *
+ * @param radix
+ * the radix used to translate the token into a BigInteger
+ * @return true iff the next token in this scanner's input can be translated
+ * into a valid BigInteger
+ * @throws IllegalStateException
+ * if the scanner has been closed
+ */
public boolean hasNextBigInteger(int radix) {
- throw new NotYetImplementedException();
+ Pattern integerPattern = getIntegerPattern(radix);
+ boolean isBigIntegerValue = false;
+ if (hasNext(integerPattern)) {
+ String intString = matcher.group();
+ intString = removeLocaleInfo(intString, DataType.INT);
+ try {
+ new BigInteger(intString, radix);
+ isBigIntegerValue = true;
+ } catch (NumberFormatException e) {
+ matchSuccessful = false;
+ }
+ }
+ return isBigIntegerValue;
}
/**
@@ -515,14 +547,46 @@
return hasNext(BOOLEAN_PATTERN);
}
- //TODO: To implement this feature
+ /**
+ * Returns true if this scanner's next token can be translated into a valid
+ * byte value in the default radix. The scanner does not advance past the
+ * input.
+ *
+ * @return true iff the next token in this scanner's input can be translated
+ * into a valid byte value
+ * @throws IllegalStateException
+ * if the scanner has been closed
+ */
public boolean hasNextByte() {
- throw new NotYetImplementedException();
+ return hasNextByte(integerRadix);
}
- //TODO: To implement this feature
+ /**
+ * Returns true if this scanner's next token can be translated into a valid
+ * byte value in the specified radix. The scanner does not advance past the
+ * input.
+ *
+ * @param radix
+ * the radix used to translate the token into a byte value
+ * @return true iff the next token in this scanner's input can be translated
+ * into a valid byte value
+ * @throws IllegalStateException
+ * if the scanner has been closed
+ */
public boolean hasNextByte(int radix) {
- throw new NotYetImplementedException();
+ Pattern integerPattern = getIntegerPattern(radix);
+ boolean isByteValue = false;
+ if (hasNext(integerPattern)) {
+ String intString = matcher.group();
+ intString = removeLocaleInfo(intString, DataType.INT);
+ try {
+ Byte.parseByte(intString, radix);
+ isByteValue = true;
+ } catch (NumberFormatException e) {
+ matchSuccessful = false;
+ }
+ }
+ return isByteValue;
}
//TODO: To implement this feature
@@ -658,7 +722,7 @@
*
* @return the match result of the last match operation
* @throws IllegalStateException
- * if the match result is available
+ * if the match result is not available
*/
public MatchResult match() {
if (!matchSuccessful) {
@@ -730,7 +794,7 @@
* advances past the next token that matched the pattern.
*
* The invocation of this method in the form next(pattern) behaves in the
- * same way as the invocaiton of next(Pattern.compile(pattern)).
+ * same way as the invocation of next(Pattern.compile(pattern)).
*
* @param pattern
* the string specifying the pattern to scan for
@@ -750,14 +814,70 @@
throw new NotYetImplementedException();
}
- //TODO: To implement this feature
+ /**
+ * Translates the next token in this scanner's input into a BigInteger and
+ * returns this value. This method may be blocked when it is waiting for
+ * input to scan, even if a previous invocation of hasNextBigInteger()
+ * returned true. If this match succeeds, the scanner advances past the
+ * input that matched.
+ *
+ * The invocation of this method in the form nextBigInteger() behaves in the
+ * same way as the invocation of nextBigInteger(radix), the radix is the
+ * default radix of this scanner.
+ *
+ * @return the BigInteger scanned from the input
+ * @throws IllegalStateException
+ * if this scanner has been closed
+ * @throws NoSuchElementException
+ * if input has been exhausted
+ * @throws InputMismatchException
+ * if the next token can not be translated into a valid
+ * BigInteger, or it is out of range
+ */
public BigInteger nextBigInteger() {
- throw new NotYetImplementedException();
+ return nextBigInteger(integerRadix);
}
- //TODO: To implement this feature
+ /**
+ * Translates the next token in this scanner's input into a BigInteger and
+ * returns this value. This method may be blocked when it is waiting for
+ * input to scan, even if a previous invocation of hasNextBigInteger(radix)
+ * returned true. If this match succeeds, the scanner advances past the
+ * input that matched.
+ *
+ * If the next token matches the Integer regular expression successfully,
+ * the token is translated into a BigInteger as following steps. At first
+ * all locale specific prefixes ,group separators, and locale specific
+ * suffixes are removed. Then non-ASCII digits are mapped into ASCII digits
+ * via {@link Character#digit(char, int)}}, a negative sign (-) is added if
+ * the locale specific negative prefixes and suffixes were present. At last
+ * the resulting String is passed to
+ * {@link BigInteger#BigInteger(String, int)}} with the specified radix.
+ *
+ * @param radix
+ * the radix used to translate the token into a BigInteger
+ * @return the int value scanned from the input
+ * @throws IllegalStateException
+ * if this scanner has been closed
+ * @throws NoSuchElementException
+ * if input has been exhausted
+ * @throws InputMismatchException
+ * if the next token can not be translated into a valid
+ * BigInteger, or it is out of range
+ */
public BigInteger nextBigInteger(int radix) {
- throw new NotYetImplementedException();
+ Pattern integerPattern = getIntegerPattern(radix);
+ String intString = next(integerPattern);
+ intString = removeLocaleInfo(intString, DataType.INT);
+ BigInteger bigIntegerValue;
+ try {
+ bigIntegerValue = new BigInteger(intString, radix);
+ } catch (NumberFormatException e) {
+ matchSuccessful = false;
+ recoverPreviousStatus();
+ throw new InputMismatchException();
+ }
+ return bigIntegerValue;
}
/**
@@ -780,14 +900,70 @@
return Boolean.parseBoolean(next(BOOLEAN_PATTERN));
}
- //TODO: To implement this feature
+ /**
+ * Translates the next token in this scanner's input into a byte value and
+ * returns this value. This method may be blocked when it is waiting for
+ * input to scan, even if a previous invocation of hasNextByte() returned
+ * true. If this match succeeds, the scanner advances past the input that
+ * matched.
+ *
+ * The invocation of this method in the form nextByte() behaves in the same
+ * way as the invocation of nextByte(radix), the radix is the default radix
+ * of this scanner.
+ *
+ * @return the byte value scanned from the input
+ * @throws IllegalStateException
+ * if this scanner has been closed
+ * @throws NoSuchElementException
+ * if input has been exhausted
+ * @throws InputMismatchException
+ * if the next token can not be translated into a valid byte
+ * value, or it is out of range
+ */
public byte nextByte() {
- throw new NotYetImplementedException();
+ return nextByte(integerRadix);
}
- //TODO: To implement this feature
+ /**
+ * Translates the next token in this scanner's input into a byte value and
+ * returns this value. This method may be blocked when it is waiting for
+ * input to scan, even if a previous invocation of hasNextByte(radix)
+ * returned true. If this match succeeds, the scanner advances past the
+ * input that matched.
+ *
+ * If the next token matches the Integer regular expression successfully,
+ * the token is translated into a byte value as following steps. At first
+ * all locale specific prefixes ,group separators, and locale specific
+ * suffixes are removed. Then non-ASCII digits are mapped into ASCII digits
+ * via {@link Character#digit(char, int)}}, a negative sign (-) is added if
+ * the locale specific negative prefixes and suffixes were present. At last
+ * the resulting String is passed to {@link Byte#parseByte(String, int)}}
+ * with the specified radix.
+ *
+ * @param radix
+ * the radix used to translate the token into byte value
+ * @return the byte value scanned from the input
+ * @throws IllegalStateException
+ * if this scanner has been closed
+ * @throws NoSuchElementException
+ * if input has been exhausted
+ * @throws InputMismatchException
+ * if the next token can not be translated into a valid byte
+ * value, or it is out of range
+ */
public byte nextByte(int radix) {
- throw new NotYetImplementedException();
+ Pattern integerPattern = getIntegerPattern(radix);
+ String intString = next(integerPattern);
+ intString = removeLocaleInfo(intString, DataType.INT);
+ byte byteValue = 0;
+ try {
+ byteValue = Byte.parseByte(intString, radix);
+ } catch (NumberFormatException e) {
+ matchSuccessful = false;
+ recoverPreviousStatus();
+ throw new InputMismatchException();
+ }
+ return byteValue;
}
//TODO: To implement this feature
@@ -844,7 +1020,7 @@
* matched.
*
* The invocation of this method in the form nextInt() behaves in the same
- * way as the invocaiton of nextInt(radix), the radix is the default radix
+ * way as the invocation of nextInt(radix), the radix is the default radix
* of this scanner.
*
* @return the int value scanned from the input
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java?rev=421880&r1=421879&r2=421880&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java Fri Jul 14 04:49:00 2006
@@ -24,6 +24,7 @@
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.StringReader;
+import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
@@ -1308,6 +1309,167 @@
/**
* @throws IOException
+ * @tests java.util.Scanner#nextByte(int)
+ */
+ public void test_nextByteI() throws IOException {
+ s = new Scanner("123 126");
+ assertEquals(123, s.nextByte(10));
+ assertEquals(126, s.nextByte(10));
+ try {
+ s.nextByte(10);
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // If the radix is different from 10
+ s = new Scanner("123 126");
+ assertEquals(38, s.nextByte(5));
+ try {
+ s.nextByte(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // If the number is out of range
+ s = new Scanner("1234");
+ try {
+ s.nextByte(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 12\u0666");
+ assertEquals(102, s.nextByte(10));
+ try {
+ s.nextByte(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ assertEquals(126, s.nextByte(10));
+
+ s = new Scanner("012");
+ assertEquals(12, s.nextByte(10));
+
+ s = new Scanner("E");
+ assertEquals(14, s.nextByte(16));
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("100");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(100, s.nextByte(10));
+
+ s = new Scanner("1\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(100, s.nextByte(10));
+
+ s = new Scanner("1\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(100, s.nextByte(10));
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertEquals(-123, s.nextByte(10));
+
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertEquals(-123, s.nextByte(10));
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#nextByte()
+ */
+ public void test_nextByte() throws IOException {
+ s = new Scanner("123 126");
+ assertEquals(123, s.nextByte());
+ assertEquals(126, s.nextByte());
+ try {
+ s.nextByte();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // If the radix is different from 10
+ s = new Scanner("123 126");
+ s.useRadix(5);
+ assertEquals(38, s.nextByte());
+ try {
+ s.nextByte();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // If the number is out of range
+ s = new Scanner("1234");
+ try {
+ s.nextByte();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 12\u0666");
+ assertEquals(102, s.nextByte());
+ s.useRadix(5);
+ try {
+ s.nextByte();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useRadix(10);
+ assertEquals(126, s.nextByte());
+
+ s = new Scanner("012");
+ assertEquals(12, s.nextByte());
+
+ s = new Scanner("E");
+ s.useRadix(16);
+ assertEquals(14, s.nextByte());
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("100");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(100, s.nextByte());
+
+ s = new Scanner("1\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(100, s.nextByte());
+
+ s = new Scanner("1\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(100, s.nextByte());
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertEquals(-123, s.nextByte());
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertEquals(-123, s.nextByte());
+ }
+
+ /**
+ * @throws IOException
* @tests java.util.Scanner#nextFloat()
*/
public void test_nextFloat() throws IOException {
@@ -1403,143 +1565,414 @@
/**
* @throws IOException
- * @tests java.util.Scanner#hasNext()
+ * @tests java.util.Scanner#nextBigInteger(int)
*/
- public void test_hasNext() throws IOException {
- s = new Scanner("1##2").useDelimiter("\\#");
- assertTrue(s.hasNext());
- assertEquals("1", s.next());
- assertEquals("", s.next());
- assertEquals("2", s.next());
- assertFalse(s.hasNext());
- s.close();
- try {
- s.hasNext();
- fail("should throw IllegalStateException");
- } catch (IllegalStateException e) {
- // expected
- }
-
- s = new Scanner("1( )2( )").useDelimiter("\\( \\)");
- assertTrue(s.hasNext());
- assertTrue(s.hasNext());
- assertEquals("1", s.next());
- assertEquals("2", s.next());
-
- s = new Scanner("1 2 ").useDelimiter("( )");
- assertEquals("1", s.next());
- assertEquals("2", s.next());
- assertTrue(s.hasNext());
- assertEquals("", s.next());
-
- s = new Scanner("1\n2 ");
- assertEquals("1", s.next());
- assertTrue(s.hasNext());
- assertEquals("2", s.next());
- assertFalse(s.hasNext());
- // test boundary case
+ public void test_nextBigIntegerI() throws IOException {
+ s = new Scanner("123 456");
+ assertEquals(new BigInteger("123"), s.nextBigInteger(10));
+ assertEquals(new BigInteger("456"), s.nextBigInteger(10));
try {
- s.next();
- fail("should throw NoSuchElementException");
+ s.nextBigInteger(10);
+ fail("Should throw NoSuchElementException");
} catch (NoSuchElementException e) {
// Expected
}
- s = new Scanner("1'\n'2 ");
- assertEquals("1'", s.next());
- assertTrue(s.hasNext());
- assertEquals("'2", s.next());
- assertFalse(s.hasNext());
- // test boundary case
+ // If the radix is different from 10
+ s = new Scanner("123 456");
+ assertEquals(new BigInteger("38"), s.nextBigInteger(5));
try {
- s.next();
- fail("should throw NoSuchElementException");
- } catch (NoSuchElementException e) {
+ s.nextBigInteger(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
// Expected
}
- s = new Scanner(" ");
- assertFalse(s.hasNext());
-
- // test socket inputStream
-
- os.write("1 2".getBytes());
- serverSocket.close();
-
- s = new Scanner(client);
- assertEquals("1", s.next());
- assertTrue(s.hasNext());
- assertEquals("2", s.next());
- assertFalse(s.hasNext());
+ /*
+ * 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);
try {
- s.next();
- fail("should throw NoSuchElementException");
- } catch (NoSuchElementException e) {
+ s.nextBigInteger(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
// Expected
}
- }
-
- /**
- * @throws IOException
- * @tests java.util.Scanner#hasNext(Pattern)
- */
- public void test_hasNextLPattern() throws IOException {
- Pattern pattern;
- s = new Scanner("aab@2@abb@").useDelimiter("\\@");
- pattern = Pattern.compile("a*b");
- assertTrue(s.hasNext(pattern));
- assertEquals("aab", s.next(pattern));
- assertFalse(s.hasNext(pattern));
+ s.useLocale(Locale.ENGLISH);
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+
+ /*
+ * ''' is used in many locales as group separator.
+ */
+ s = new Scanner("23'456 23'456");
+ s.useLocale(Locale.GERMANY);
try {
- s.next(pattern);
- fail("should throw InputMismatchException");
+ s.nextBigInteger(10);
+ fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
+ s.useLocale(new Locale("it", "CH"));
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
- s = new Scanner("word ? ");
- pattern = Pattern.compile("\\w+");
- assertTrue(s.hasNext(pattern));
- assertEquals("word", s.next(pattern));
- assertFalse(s.hasNext(pattern));
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 1\u06662");
+ assertEquals(new BigInteger("102"), s.nextBigInteger(10));
try {
- s.next(pattern);
- fail("should throw InputMismatchException");
+ s.nextBigInteger(5);
+ fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
+ assertEquals(new BigInteger("162"), s.nextBigInteger(10));
- s = new Scanner("word1 WorD2 ");
- pattern = Pattern.compile("\\w+");
- assertTrue(s.hasNext(pattern));
- assertEquals("word1", s.next(pattern));
- assertTrue(s.hasNext(pattern));
- assertEquals("WorD2", s.next(pattern));
- assertFalse(s.hasNext(pattern));
+ /*
+ * '.' is used in many locales as group separator. The input string
+ * has Arabic-Indic digits .
+ */
+ s = new Scanner("23.45\u0666 23.456");
+ s.useLocale(Locale.CHINESE);
try {
- s.next(pattern);
- fail("should throw NoSuchElementException");
- } catch (NoSuchElementException e) {
+ s.nextBigInteger(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
// Expected
}
+ s.useLocale(Locale.GERMANY);
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
- s = new Scanner("word1 WorD2 ");
- pattern = Pattern.compile("\\w+");
- try {
- s.hasNext((Pattern) null);
- fail("Should throw NullPointerException");
- } catch (NullPointerException e) {
- // expected
- }
- s.close();
+ // The input string starts with zero
+ s = new Scanner("03,456");
+ s.useLocale(Locale.ENGLISH);
try {
- s.hasNext(pattern);
- fail("should throw IllegalStateException");
- } catch (IllegalStateException e) {
- // expected
+ s.nextBigInteger(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
}
-
- // test socket inputStream
+
+ s = new Scanner("03456");
+ assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
+
+ s = new Scanner("\u06603,456");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
+
+ s = new Scanner("E34");
+ assertEquals(new BigInteger("3636"), s.nextBigInteger(16));
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("12300");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
+
+ s = new Scanner("123\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
+
+ s = new Scanner("123\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
+
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#nextBigInteger()
+ */
+ public void test_nextBigInteger() throws IOException {
+ s = new Scanner("123 456");
+ assertEquals(new BigInteger("123"), s.nextBigInteger());
+ assertEquals(new BigInteger("456"), s.nextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // If the radix is different from 10
+ s = new Scanner("123 456");
+ s.useRadix(5);
+ assertEquals(new BigInteger("38"), s.nextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ /*
+ * 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);
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(Locale.ENGLISH);
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+
+ /*
+ * ''' is used in many locales as group separator.
+ */
+ s = new Scanner("23'456 23'456");
+ s.useLocale(Locale.GERMANY);
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(new Locale("it", "CH"));
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 1\u06662");
+ assertEquals(new BigInteger("102"), s.nextBigInteger());
+ s.useRadix(5);
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useRadix(10);
+ assertEquals(new BigInteger("162"), s.nextBigInteger());
+
+ /*
+ * '.' is used in many locales as group separator. The input string
+ * has Arabic-Indic digits .
+ */
+ s = new Scanner("23.45\u0666 23.456");
+ s.useLocale(Locale.CHINESE);
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(Locale.GERMANY);
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+
+ // The input string starts with zero
+ s = new Scanner("03,456");
+ s.useLocale(Locale.ENGLISH);
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ s = new Scanner("03456");
+ assertEquals(new BigInteger("3456"), s.nextBigInteger());
+
+ s = new Scanner("\u06603,456");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(new BigInteger("3456"), s.nextBigInteger());
+
+ s = new Scanner("E34");
+ s.useRadix(16);
+ assertEquals(new BigInteger("3636"), s.nextBigInteger());
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("12300");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(new BigInteger("12300"), s.nextBigInteger());
+
+ s = new Scanner("123\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(new BigInteger("12300"), s.nextBigInteger());
+
+ s = new Scanner("123\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(new BigInteger("12300"), s.nextBigInteger());
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertEquals(new BigInteger("-123"), s.nextBigInteger());
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertEquals(new BigInteger("-123"), s.nextBigInteger());
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNext()
+ */
+ public void test_hasNext() throws IOException {
+ s = new Scanner("1##2").useDelimiter("\\#");
+ assertTrue(s.hasNext());
+ assertEquals("1", s.next());
+ assertEquals("", s.next());
+ assertEquals("2", s.next());
+ assertFalse(s.hasNext());
+ s.close();
+ try {
+ s.hasNext();
+ fail("should throw IllegalStateException");
+ } catch (IllegalStateException e) {
+ // expected
+ }
+
+ s = new Scanner("1( )2( )").useDelimiter("\\( \\)");
+ assertTrue(s.hasNext());
+ assertTrue(s.hasNext());
+ assertEquals("1", s.next());
+ assertEquals("2", s.next());
+
+ s = new Scanner("1 2 ").useDelimiter("( )");
+ assertEquals("1", s.next());
+ assertEquals("2", s.next());
+ assertTrue(s.hasNext());
+ assertEquals("", s.next());
+
+ s = new Scanner("1\n2 ");
+ assertEquals("1", s.next());
+ assertTrue(s.hasNext());
+ assertEquals("2", s.next());
+ assertFalse(s.hasNext());
+ // test boundary case
+ try {
+ s.next();
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ s = new Scanner("1'\n'2 ");
+ assertEquals("1'", s.next());
+ assertTrue(s.hasNext());
+ assertEquals("'2", s.next());
+ assertFalse(s.hasNext());
+ // test boundary case
+ try {
+ s.next();
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ s = new Scanner(" ");
+ assertFalse(s.hasNext());
+
+ // test socket inputStream
+
+ os.write("1 2".getBytes());
+ serverSocket.close();
+
+ s = new Scanner(client);
+ assertEquals("1", s.next());
+ assertTrue(s.hasNext());
+ assertEquals("2", s.next());
+ assertFalse(s.hasNext());
+ try {
+ s.next();
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNext(Pattern)
+ */
+ public void test_hasNextLPattern() throws IOException {
+ Pattern pattern;
+ s = new Scanner("aab@2@abb@").useDelimiter("\\@");
+ pattern = Pattern.compile("a*b");
+ assertTrue(s.hasNext(pattern));
+ assertEquals("aab", s.next(pattern));
+ assertFalse(s.hasNext(pattern));
+ try {
+ s.next(pattern);
+ fail("should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ s = new Scanner("word ? ");
+ pattern = Pattern.compile("\\w+");
+ assertTrue(s.hasNext(pattern));
+ assertEquals("word", s.next(pattern));
+ assertFalse(s.hasNext(pattern));
+ try {
+ s.next(pattern);
+ fail("should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ s = new Scanner("word1 WorD2 ");
+ pattern = Pattern.compile("\\w+");
+ assertTrue(s.hasNext(pattern));
+ assertEquals("word1", s.next(pattern));
+ assertTrue(s.hasNext(pattern));
+ assertEquals("WorD2", s.next(pattern));
+ assertFalse(s.hasNext(pattern));
+ try {
+ s.next(pattern);
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ s = new Scanner("word1 WorD2 ");
+ pattern = Pattern.compile("\\w+");
+ try {
+ s.hasNext((Pattern) null);
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+ s.close();
+ try {
+ s.hasNext(pattern);
+ fail("should throw IllegalStateException");
+ } catch (IllegalStateException e) {
+ // expected
+ }
+
+ // test socket inputStream
os.write("aab b".getBytes());
serverSocket.close();
@@ -1558,119 +1991,633 @@
/**
* @throws IOException
- * @tests java.util.Scanner#hasNext(String)
+ * @tests java.util.Scanner#hasNext(String)
+ */
+ public void test_hasNextLString() throws IOException {
+ s = new Scanner("aab@2@abb@").useDelimiter("\\@");
+ try {
+ s.hasNext((String)null);
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ s = new Scanner("aab*b*").useDelimiter("\\*");
+ assertTrue(s.hasNext("a+b"));
+ assertEquals("aab", s.next("a+b"));
+ assertFalse(s.hasNext("a+b"));
+ try {
+ s.next("a+b");
+ fail("should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.close();
+ try {
+ s.hasNext("a+b");
+ fail("should throw IllegalStateException");
+ } catch (IllegalStateException e) {
+ // expected
+ }
+
+ s = new Scanner("WORD ? ");
+ assertTrue(s.hasNext("\\w+"));
+ assertEquals("WORD", s.next("\\w+"));
+ assertFalse(s.hasNext("\\w+"));
+ try {
+ s.next("\\w+");
+ fail("should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ s = new Scanner("word1 word2 ");
+ assertEquals("word1", s.next("\\w+"));
+ assertEquals("word2", s.next("\\w+"));
+ // test boundary case
+ try {
+ s.next("\\w+");
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // test socket inputStream
+
+ os.write("aab 2".getBytes());
+ serverSocket.close();
+
+ s = new Scanner(client);
+ assertTrue(s.hasNext("a*b"));
+ assertEquals("aab", s.next("a*b"));
+ assertFalse(s.hasNext("a*b"));
+ try {
+ s.next("a*b");
+ fail("should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNextBoolean()
+ */
+ public void test_hasNextBoolean() throws IOException {
+
+ s = new Scanner("TRue");
+ assertTrue(s.hasNextBoolean());
+ assertTrue(s.nextBoolean());
+
+ s = new Scanner("tRue false");
+ assertTrue(s.hasNextBoolean());
+ assertTrue(s.nextBoolean());
+ assertTrue(s.hasNextBoolean());
+ assertFalse(s.nextBoolean());
+
+ s = new Scanner("");
+ assertFalse(s.hasNextBoolean());
+
+ // test socket inputStream
+
+ os.write("true false ".getBytes());
+ serverSocket.close();
+
+ s = new Scanner(client);
+ assertTrue(s.hasNextBoolean());
+ assertTrue(s.nextBoolean());
+
+ // ues '*' as delimiter
+ s = new Scanner("true**false").useDelimiter("\\*");
+ assertTrue(s.hasNextBoolean());
+ assertTrue(s.nextBoolean());
+ assertFalse(s.hasNextBoolean());
+ try {
+ s.nextBoolean();
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ s = new Scanner("false( )").useDelimiter("\\( \\)");
+ assertTrue(s.hasNextBoolean());
+ assertFalse(s.nextBoolean());
+ assertFalse(s.hasNextBoolean());
+
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNextByte(int)
+ */
+ public void test_hasNextByteI() throws IOException {
+ s = new Scanner("123 126");
+ assertTrue(s.hasNextByte(10));
+ assertEquals(123, s.nextByte(10));
+ assertTrue(s.hasNextByte(10));
+ assertEquals(126, s.nextByte(10));
+ assertFalse(s.hasNextByte(10));
+ try {
+ s.nextByte(10);
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // If the radix is different from 10
+ s = new Scanner("123 126");
+ assertTrue(s.hasNextByte(5));
+ assertEquals(38, s.nextByte(5));
+ assertFalse(s.hasNextByte(5));
+ try {
+ s.nextByte(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // If the number is out of range
+ s = new Scanner("1234");
+ assertFalse(s.hasNextByte(10));
+ try {
+ s.nextByte(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 12\u0666");
+ assertTrue(s.hasNextByte(10));
+ assertEquals(102, s.nextByte(10));
+ assertFalse(s.hasNextByte(5));
+ try {
+ s.nextByte(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ assertTrue(s.hasNextByte(10));
+ assertEquals(126, s.nextByte(10));
+
+ s = new Scanner("012");
+ assertTrue(s.hasNextByte(10));
+ assertEquals(12, s.nextByte(10));
+
+ s = new Scanner("E");
+ assertTrue(s.hasNextByte(16));
+ assertEquals(14, s.nextByte(16));
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("100");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextByte(10));
+ assertEquals(100, s.nextByte(10));
+
+ s = new Scanner("1\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextByte(10));
+ assertEquals(100, s.nextByte(10));
+
+ s = new Scanner("1\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextByte(10));
+ assertEquals(100, s.nextByte(10));
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertTrue(s.hasNextByte(10));
+ assertEquals(-123, s.nextByte(10));
+
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertTrue(s.hasNextByte(10));
+ assertEquals(-123, s.nextByte(10));
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNextByte()
+ */
+ public void test_hasNextByte() throws IOException {
+ s = new Scanner("123 126");
+ assertTrue(s.hasNextByte());
+ assertEquals(123, s.nextByte());
+ assertTrue(s.hasNextByte());
+ assertEquals(126, s.nextByte());
+ assertFalse(s.hasNextByte());
+ try {
+ s.nextByte();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // If the radix is different from 10
+ s = new Scanner("123 126");
+ s.useRadix(5);
+ assertTrue(s.hasNextByte());
+ assertEquals(38, s.nextByte());
+ assertFalse(s.hasNextByte());
+ try {
+ s.nextByte();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // If the number is out of range
+ s = new Scanner("1234");
+ assertFalse(s.hasNextByte());
+ try {
+ s.nextByte();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 12\u0666");
+ assertTrue(s.hasNextByte());
+ assertEquals(102, s.nextByte());
+ s.useRadix(5);
+ assertFalse(s.hasNextByte());
+ try {
+ s.nextByte();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useRadix(10);
+ assertTrue(s.hasNextByte());
+ assertEquals(126, s.nextByte());
+
+ s = new Scanner("012");
+ assertEquals(12, s.nextByte());
+
+ s = new Scanner("E");
+ s.useRadix(16);
+ assertTrue(s.hasNextByte());
+ assertEquals(14, s.nextByte());
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("100");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextByte());
+ assertEquals(100, s.nextByte());
+
+ s = new Scanner("1\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextByte());
+ assertEquals(100, s.nextByte());
+
+ s = new Scanner("1\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextByte());
+ assertEquals(100, s.nextByte());
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertTrue(s.hasNextByte());
+ assertEquals(-123, s.nextByte());
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertTrue(s.hasNextByte());
+ assertEquals(-123, s.nextByte());
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNextBigInteger(int)
*/
- public void test_hasNextLString() throws IOException {
- s = new Scanner("aab@2@abb@").useDelimiter("\\@");
+ public void test_hasNextBigIntegerI() throws IOException {
+ s = new Scanner("123 456");
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("123"), s.nextBigInteger(10));
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("456"), s.nextBigInteger(10));
+ assertFalse(s.hasNextBigInteger(10));
try {
- s.hasNext((String)null);
- fail("Should throw NullPointerException");
- } catch (NullPointerException e) {
- // expected
+ s.nextBigInteger(10);
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
}
-
- s = new Scanner("aab*b*").useDelimiter("\\*");
- assertTrue(s.hasNext("a+b"));
- assertEquals("aab", s.next("a+b"));
- assertFalse(s.hasNext("a+b"));
+
+ // If the radix is different from 10
+ s = new Scanner("123 456");
+ assertTrue(s.hasNextBigInteger(5));
+ assertEquals(new BigInteger("38"), s.nextBigInteger(5));
+ assertFalse(s.hasNextBigInteger(5));
try {
- s.next("a+b");
- fail("should throw InputMismatchException");
+ s.nextBigInteger(5);
+ fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
- s.close();
+
+ /*
+ * 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.hasNextBigInteger(10));
try {
- s.hasNext("a+b");
- fail("should throw IllegalStateException");
- } catch (IllegalStateException e) {
- // expected
+ s.nextBigInteger(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
}
+ s.useLocale(Locale.ENGLISH);
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
- s = new Scanner("WORD ? ");
- assertTrue(s.hasNext("\\w+"));
- assertEquals("WORD", s.next("\\w+"));
- assertFalse(s.hasNext("\\w+"));
+ /*
+ * ''' is used in many locales as group separator.
+ */
+ s = new Scanner("23'456 23'456");
+ s.useLocale(Locale.GERMANY);
+ assertFalse(s.hasNextBigInteger(10));
try {
- s.next("\\w+");
- fail("should throw InputMismatchException");
+ s.nextBigInteger(10);
+ fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
+ s.useLocale(new Locale("it", "CH"));
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
- s = new Scanner("word1 word2 ");
- assertEquals("word1", s.next("\\w+"));
- assertEquals("word2", s.next("\\w+"));
- // test boundary case
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 1\u06662");
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("102"), s.nextBigInteger(10));
+ assertFalse(s.hasNextBigInteger(5));
try {
- s.next("\\w+");
- fail("should throw NoSuchElementException");
- } catch (NoSuchElementException e) {
+ s.nextBigInteger(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
// Expected
}
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("162"), s.nextBigInteger(10));
- // test socket inputStream
-
- os.write("aab 2".getBytes());
- serverSocket.close();
+ /*
+ * '.' is used in many locales as group separator. The input string
+ * has Arabic-Indic digits .
+ */
+ s = new Scanner("23.45\u0666 23.456");
+ s.useLocale(Locale.CHINESE);
+ assertFalse(s.hasNextBigInteger(10));
+ try {
+ s.nextBigInteger(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(Locale.GERMANY);
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
- s = new Scanner(client);
- assertTrue(s.hasNext("a*b"));
- assertEquals("aab", s.next("a*b"));
- assertFalse(s.hasNext("a*b"));
+ // The input string starts with zero
+ s = new Scanner("03,456");
+ s.useLocale(Locale.ENGLISH);
+ assertFalse(s.hasNextBigInteger(10));
try {
- s.next("a*b");
- fail("should throw InputMismatchException");
+ s.nextBigInteger(10);
+ fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
+
+ s = new Scanner("03456");
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
+
+ s = new Scanner("\u06603,456");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
+
+ s = new Scanner("E34");
+ assertTrue(s.hasNextBigInteger(16));
+ assertEquals(new BigInteger("3636"), s.nextBigInteger(16));
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("12300");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
+
+ s = new Scanner("123\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
+
+ s = new Scanner("123\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
+
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
}
/**
* @throws IOException
- * @tests java.util.Scanner#hasNextBoolean()
+ * @tests java.util.Scanner#hasNextBigInteger()
*/
- public void test_hasNextBoolean() throws IOException {
-
- s = new Scanner("TRue");
- assertTrue(s.hasNextBoolean());
- assertTrue(s.nextBoolean());
+ public void test_hasNextBigInteger() throws IOException {
+ s = new Scanner("123 456");
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("123"), s.nextBigInteger());
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("456"), s.nextBigInteger());
+ assertFalse(s.hasNextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
- s = new Scanner("tRue false");
- assertTrue(s.hasNextBoolean());
- assertTrue(s.nextBoolean());
- assertTrue(s.hasNextBoolean());
- assertFalse(s.nextBoolean());
+ // If the radix is different from 10
+ s = new Scanner("123 456");
+ s.useRadix(5);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("38"), s.nextBigInteger());
+ assertFalse(s.hasNextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
- s = new Scanner("");
- assertFalse(s.hasNextBoolean());
+ /*
+ * 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.hasNextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(Locale.ENGLISH);
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
- // test socket inputStream
+ /*
+ * ''' is used in many locales as group separator.
+ */
+ s = new Scanner("23'456 23'456");
+ s.useLocale(Locale.GERMANY);
+ assertFalse(s.hasNextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(new Locale("it", "CH"));
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
- os.write("true false ".getBytes());
- serverSocket.close();
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 1\u06662");
+ assertEquals(new BigInteger("102"), s.nextBigInteger());
+ s.useRadix(5);
+ assertFalse(s.hasNextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useRadix(10);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("162"), s.nextBigInteger());
- s = new Scanner(client);
- assertTrue(s.hasNextBoolean());
- assertTrue(s.nextBoolean());
+ /*
+ * '.' is used in many locales as group separator. The input string
+ * has Arabic-Indic digits .
+ */
+ s = new Scanner("23.45\u0666 23.456");
+ s.useLocale(Locale.CHINESE);
+ assertFalse(s.hasNextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(Locale.GERMANY);
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
- // ues '*' as delimiter
- s = new Scanner("true**false").useDelimiter("\\*");
- assertTrue(s.hasNextBoolean());
- assertTrue(s.nextBoolean());
- assertFalse(s.hasNextBoolean());
+ // The input string starts with zero
+ s = new Scanner("03,456");
+ s.useLocale(Locale.ENGLISH);
+ assertFalse(s.hasNextBigInteger());
try {
- s.nextBoolean();
- fail("should throw NoSuchElementException");
- } catch (NoSuchElementException e) {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
// Expected
}
- s = new Scanner("false( )").useDelimiter("\\( \\)");
- assertTrue(s.hasNextBoolean());
- assertFalse(s.nextBoolean());
- assertFalse(s.hasNextBoolean());
+ s = new Scanner("03456");
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("3456"), s.nextBigInteger());
+
+ s = new Scanner("\u06603,456");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("3456"), s.nextBigInteger());
+
+ s = new Scanner("E34");
+ s.useRadix(16);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("3636"), s.nextBigInteger());
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("12300");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("12300"), s.nextBigInteger());
+
+ s = new Scanner("123\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("12300"), s.nextBigInteger());
+
+ s = new Scanner("123\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("12300"), s.nextBigInteger());
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("-123"), s.nextBigInteger());
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("-123"), s.nextBigInteger());
}
/**