You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by py...@apache.org on 2006/07/19 16:00:08 UTC
svn commit: r423464 - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/util/Scanner.java
test/java/tests/api/java/util/ScannerTest.java
Author: pyang
Date: Wed Jul 19 07:00:07 2006
New Revision: 423464
URL: http://svn.apache.org/viewvc?rev=423464&view=rev
Log:
Fix for HARMONY-918 ([classlib][luni] Implementation of 4 nextXXXX 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=423464&r1=423463&r2=423464&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 Wed Jul 19 07:00:07 2006
@@ -495,9 +495,29 @@
return hasNext(Pattern.compile(pattern));
}
- //TODO: To implement this feature
+ /**
+ * Returns true if this scanner's next token can be translated into a valid
+ * BigDecimal. 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 BigDecimal
+ * @throws IllegalStateException
+ * if the scanner has been closed
+ */
public boolean hasNextBigDecimal() {
- throw new NotYetImplementedException();
+ Pattern floatPattern = getFloatPattern();
+ boolean isBigDecimalValue = false;
+ if (hasNext(floatPattern)) {
+ String floatString = matcher.group();
+ floatString = removeLocaleInfoFromFloat(floatString);
+ try {
+ new BigDecimal(floatString);
+ isBigDecimalValue = true;
+ } catch (NumberFormatException e) {
+ matchSuccessful = false;
+ }
+ }
+ return isBigDecimalValue;
}
/**
@@ -598,9 +618,29 @@
return isByteValue;
}
- //TODO: To implement this feature
+ /**
+ * Returns true if this scanner's next token can be translated into a valid
+ * double value. 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 double value
+ * @throws IllegalStateException
+ * if the scanner has been closed
+ */
public boolean hasNextDouble() {
- throw new NotYetImplementedException();
+ Pattern floatPattern = getFloatPattern();
+ boolean isDoubleValue = false;
+ if (hasNext(floatPattern)) {
+ String floatString = matcher.group();
+ floatString = removeLocaleInfoFromFloat(floatString);
+ try {
+ Double.parseDouble(floatString);
+ isDoubleValue = true;
+ } catch (NumberFormatException e) {
+ matchSuccessful = false;
+ }
+ }
+ return isDoubleValue;
}
/**
@@ -883,9 +923,45 @@
return next(Pattern.compile(pattern));
}
- //TODO: To implement this feature
+ /**
+ * Translates the next token in this scanner's input into a BigDecimal and
+ * returns this value. This method may be blocked when it is waiting for
+ * input to scan, even if a previous invocation of hasNextBigDecimal()
+ * returned true. If this match succeeds, the scanner advances past the
+ * input that matched.
+ *
+ * If the next token matches the Float regular expression successfully, the
+ * token is translated into a BigDecimal 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 BigDecimal#BigDecimal(String)}}.
+ *
+ * @param integerRadix
+ * the radix used to translate the token into BigDecimal
+ * @return the BigDecimal 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
+ * BigDecimal
+ */
public BigDecimal nextBigDecimal() {
- throw new NotYetImplementedException();
+ Pattern floatPattern = getFloatPattern();
+ String floatString = next(floatPattern);
+ floatString = removeLocaleInfoFromFloat(floatString);
+ BigDecimal bigDecimalValue;
+ try {
+ bigDecimalValue = new BigDecimal(floatString);
+ } catch (NumberFormatException e) {
+ matchSuccessful = false;
+ recoverPreviousStatus();
+ throw new InputMismatchException();
+ }
+ return bigDecimalValue;
}
/**
@@ -1040,9 +1116,47 @@
return byteValue;
}
- //TODO: To implement this feature
+ /**
+ * Translates the next token in this scanner's input into a double value and
+ * returns this value. This method may be blocked when it is waiting for
+ * input to scan, even if a previous invocation of hasNextDouble() returned
+ * true. If this match succeeds, the scanner advances past the input that
+ * matched.
+ *
+ * If the next token matches the Float regular expression successfully, the
+ * token is translated into a double 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 Double#parseDouble(String)}}.If the
+ * token matches the localized NaN or infinity strings, it is also passed to
+ * {@link Double#parseDouble(String)}}.
+ *
+ * @param integerRadix
+ * the radix used to translate the token into double value
+ * @return the double 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 double
+ * value
+ */
public double nextDouble() {
- throw new NotYetImplementedException();
+ Pattern floatPattern = getFloatPattern();
+ String floatString = next(floatPattern);
+ floatString = removeLocaleInfoFromFloat(floatString);
+ double doubleValue = 0;
+ try {
+ doubleValue = Double.parseDouble(floatString);
+ } catch (NumberFormatException e) {
+ matchSuccessful = false;
+ recoverPreviousStatus();
+ throw new InputMismatchException();
+ }
+ return doubleValue;
}
/**
@@ -1579,11 +1693,11 @@
StringBuilder nonZeroDigit = new StringBuilder("[\\p{javaDigit}&&[^0]]"); //$NON-NLS-1$
StringBuilder numeral = getNumeral(digit, nonZeroDigit);
- char decimalSeparator = decimalFormat.getDecimalFormatSymbols()
- .getDecimalSeparator();
+ String decimalSeparator = "\\" + decimalFormat.getDecimalFormatSymbols()//$NON-NLS-1$
+ .getDecimalSeparator();
StringBuilder decimalNumeral = new StringBuilder("(").append(numeral) //$NON-NLS-1$
- .append("|").append(numeral).append("\\") //$NON-NLS-1$//$NON-NLS-2$
- .append(decimalSeparator).append(digit).append("*+|\\").append( //$NON-NLS-1$
+ .append("|").append(numeral) //$NON-NLS-1$
+ .append(decimalSeparator).append(digit).append("*+|").append( //$NON-NLS-1$
decimalSeparator).append(digit).append("++)"); //$NON-NLS-1$
StringBuilder exponent = new StringBuilder("([eE][+-]?").append(digit) //$NON-NLS-1$
.append("+)?"); //$NON-NLS-1$
@@ -1595,14 +1709,14 @@
.append(addNegativeSign(decimalNumeral)).append("(").append( //$NON-NLS-1$
exponent).append("?)").append("))"); //$NON-NLS-1$ //$NON-NLS-2$
- StringBuilder hexFloat = new StringBuilder("([-+]?0[xX][0-9a-fA-F]*\\") //$NON-NLS-1$
- .append(decimalSeparator).append(
- "[0-9a-fA-F]+([pP][-+]?[0-9]++)?)"); //$NON-NLS-1$
+ StringBuilder hexFloat = new StringBuilder("([-+]?0[xX][0-9a-fA-F]*") //$NON-NLS-1$
+ .append("\\.").append(//$NON-NLS-1$
+ "[0-9a-fA-F]+([pP][-+]?[0-9]+)?)"); //$NON-NLS-1$
String localNaN = decimalFormat.getDecimalFormatSymbols().getNaN();
String localeInfinity = decimalFormat.getDecimalFormatSymbols()
.getInfinity();
- StringBuilder nonNumber = new StringBuilder("NaN|\\").append(localNaN) //$NON-NLS-1$
- .append("|Infinity|\\").append(localeInfinity).append(""); //$NON-NLS-1$ //$NON-NLS-2$
+ StringBuilder nonNumber = new StringBuilder("(NaN|\\Q").append(localNaN) //$NON-NLS-1$
+ .append("\\E|Infinity|\\Q").append(localeInfinity).append("\\E)"); //$NON-NLS-1$ //$NON-NLS-2$
StringBuilder singedNonNumber = new StringBuilder("((([-+]?(").append( //$NON-NLS-1$
nonNumber).append(")))|(").append(addPositiveSign(nonNumber)) //$NON-NLS-1$
.append(")|(").append(addNegativeSign(nonNumber)).append("))"); //$NON-NLS-1$ //$NON-NLS-2$
@@ -1615,11 +1729,12 @@
private StringBuilder getNumeral(StringBuilder digit,
StringBuilder nonZeroDigit) {
- char groupSeparator = decimalFormat.getDecimalFormatSymbols()
- .getGroupingSeparator();
+ String groupSeparator = "\\"//$NON-NLS-1$
+ + decimalFormat.getDecimalFormatSymbols()
+ .getGroupingSeparator();
StringBuilder groupedNumeral = new StringBuilder("(").append( //$NON-NLS-1$
nonZeroDigit).append(digit).append("?").append(digit).append( //$NON-NLS-1$
- "?(\\").append(groupSeparator).append(digit).append(digit) //$NON-NLS-1$
+ "?(").append(groupSeparator).append(digit).append(digit) //$NON-NLS-1$
.append(digit).append(")+)"); //$NON-NLS-1$
StringBuilder numeral = new StringBuilder("((").append(digit).append( //$NON-NLS-1$
"++)|").append(groupedNumeral).append(")"); //$NON-NLS-1$ //$NON-NLS-2$
@@ -1633,10 +1748,10 @@
String positivePrefix = ""; //$NON-NLS-1$
String positiveSuffix = ""; //$NON-NLS-1$
if (!decimalFormat.getPositivePrefix().equals("")) { //$NON-NLS-1$
- positivePrefix = "\\" + decimalFormat.getPositivePrefix(); //$NON-NLS-1$
+ positivePrefix = "\\Q" + decimalFormat.getPositivePrefix()+"\\E"; //$NON-NLS-1$ //$NON-NLS-2$
}
if (!decimalFormat.getPositiveSuffix().equals("")) { //$NON-NLS-1$
- positiveSuffix = "\\" + decimalFormat.getPositiveSuffix(); //$NON-NLS-1$
+ positiveSuffix = "\\Q" + decimalFormat.getPositiveSuffix()+"\\E"; //$NON-NLS-1$ //$NON-NLS-2$
}
StringBuilder signedNumeral = new StringBuilder()
.append(positivePrefix).append(unSignNumeral).append(
@@ -1651,10 +1766,10 @@
String negativePrefix = ""; //$NON-NLS-1$
String negativeSuffix = ""; //$NON-NLS-1$
if (!decimalFormat.getNegativePrefix().equals("")) { //$NON-NLS-1$
- negativePrefix = "\\" + decimalFormat.getNegativePrefix(); //$NON-NLS-1$
+ negativePrefix = "\\Q" + decimalFormat.getNegativePrefix()+"\\E"; //$NON-NLS-1$//$NON-NLS-2$
}
if (!decimalFormat.getNegativeSuffix().equals("")) { //$NON-NLS-1$
- negativeSuffix = "\\" + decimalFormat.getNegativeSuffix(); //$NON-NLS-1$
+ negativeSuffix = "\\Q" + decimalFormat.getNegativeSuffix()+"\\E"; //$NON-NLS-1$//$NON-NLS-2$
}
StringBuilder signedNumeral = new StringBuilder()
.append(negativePrefix).append(unSignNumeral).append(
@@ -1716,9 +1831,18 @@
}
}
if (DataType.FLOAT == type) {
- for (int i = 0; i < tokenBuilder.length(); i++) {
- if (-1 != Character.digit(tokenBuilder.charAt(i), 10)) {
- result.append(Character.digit(tokenBuilder.charAt(i), 10));
+ if (tokenBuilder.toString().equals(decimalFormat.getDecimalFormatSymbols()
+ .getNaN())) {
+ result.append("NaN");//$NON-NLS-1$
+ } else if (tokenBuilder.toString().equals(decimalFormat
+ .getDecimalFormatSymbols().getInfinity())) {
+ result.append("Infinity");//$NON-NLS-1$
+ } else {
+ for (int i = 0; i < tokenBuilder.length(); i++) {
+ if (-1 != Character.digit(tokenBuilder.charAt(i), 10)) {
+ result.append(Character.digit(tokenBuilder.charAt(i),
+ 10));
+ }
}
}
}
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=423464&r1=423463&r2=423464&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 Wed Jul 19 07:00:07 2006
@@ -24,6 +24,7 @@
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.StringReader;
+import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
@@ -4351,6 +4352,191 @@
assertEquals(-123, s.nextLong());
}
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#nextDouble()
+ */
+ public void test_hasNextDouble() throws IOException {
+ s = new Scanner("123 45\u0666. 123.4 .123 ");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextDouble());
+ assertEquals(123.0, s.nextDouble());
+ assertTrue(s.hasNextDouble());
+ assertEquals(456.0, s.nextDouble());
+ assertTrue(s.hasNextDouble());
+ assertEquals(123.4, s.nextDouble());
+ assertTrue(s.hasNextDouble());
+ assertEquals(0.123, s.nextDouble());
+ assertFalse(s.hasNextDouble());
+ try {
+ s.nextDouble();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextDouble());
+ assertEquals(123.4, s.nextDouble());
+ assertTrue(s.hasNextDouble());
+ assertEquals(-456.7, s.nextDouble());
+ assertTrue(s.hasNextDouble());
+ assertEquals(123456.789, s.nextDouble());
+ assertFalse(s.hasNextDouble());
+ try {
+ s.nextDouble();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // Scientific notation
+ s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextDouble());
+ assertEquals(1.234E12, s.nextDouble());
+ assertTrue(s.hasNextDouble());
+ assertEquals(-4.567E14, s.nextDouble());
+ assertTrue(s.hasNextDouble());
+ assertEquals(1.23456789E-5, s.nextDouble());
+
+ s = new Scanner("NaN Infinity -Infinity");
+ assertTrue(s.hasNextDouble());
+ assertEquals(Double.NaN, s.nextDouble());
+ assertTrue(s.hasNextDouble());
+ assertEquals(Double.POSITIVE_INFINITY, s.nextDouble());
+ assertTrue(s.hasNextDouble());
+ assertEquals(Double.NEGATIVE_INFINITY, s.nextDouble());
+
+ String str=String.valueOf(Double.MAX_VALUE*2);
+ s=new Scanner(str);
+ assertTrue(s.hasNextDouble());
+ assertEquals(Double.POSITIVE_INFINITY,s.nextDouble());
+
+ /*
+ * 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.ENGLISH);
+ assertTrue(s.hasNextDouble());
+ assertEquals(23456.0, s.nextDouble());
+ s.useLocale(Locale.GERMANY);
+ assertTrue(s.hasNextDouble());
+ assertEquals(23.456, s.nextDouble());
+
+ s = new Scanner("23.456 23.456");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextDouble());
+ assertEquals(23.456, s.nextDouble());
+ s.useLocale(Locale.GERMANY);
+ assertTrue(s.hasNextDouble());
+ assertEquals(23456.0, s.nextDouble());
+
+ s = new Scanner("23,456.7 23.456,7");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextDouble());
+ assertEquals(23456.7, s.nextDouble());
+ s.useLocale(Locale.GERMANY);
+ assertTrue(s.hasNextDouble());
+ assertEquals(23456.7, s.nextDouble());
+
+ s = new Scanner("-123.4");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextDouble());
+ assertEquals(-123.4, s.nextDouble());
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNextBigDecimal()
+ */
+ public void test_hasNextBigDecimal() throws IOException {
+ s = new Scanner("123 45\u0666. 123.4 .123 ");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("123"), s.nextBigDecimal());
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("456"), s.nextBigDecimal());
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("123.4"), s.nextBigDecimal());
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("0.123"), s.nextBigDecimal());
+ assertFalse(s.hasNextBigDecimal());
+ try {
+ s.nextBigDecimal();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("123.4"), s.nextBigDecimal());
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("-456.7"), s.nextBigDecimal());
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("123456.789"), s.nextBigDecimal());
+ assertFalse(s.hasNextBigDecimal());
+ try {
+ s.nextBigDecimal();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // Scientific notation
+ s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("1.234E12"), s.nextBigDecimal());
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("-4.567E14"), s.nextBigDecimal());
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("1.23456789E-5"), s.nextBigDecimal());
+
+ s = new Scanner("NaN");
+ assertFalse(s.hasNextBigDecimal());
+ try {
+ s.nextBigDecimal();
+ 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.ENGLISH);
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("23456"), s.nextBigDecimal());
+ s.useLocale(Locale.GERMANY);
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("23.456"), s.nextBigDecimal());
+
+ s = new Scanner("23.456 23.456");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("23.456"), s.nextBigDecimal());
+ s.useLocale(Locale.GERMANY);
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("23456"), s.nextBigDecimal());
+
+ s = new Scanner("23,456.7");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("23456.7"), s.nextBigDecimal());
+
+ s = new Scanner("-123.4");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextBigDecimal());
+ assertEquals(new BigDecimal("-123.4"), s.nextBigDecimal());
+ }
+
private static class MockStringReader extends StringReader {
public MockStringReader(String param) {
@@ -4743,6 +4929,154 @@
} catch (NullPointerException e) {
// expected
}
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#nextDouble()
+ */
+ public void test_nextDouble() throws IOException {
+ s = new Scanner("123 45\u0666. 123.4 .123 ");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(123.0, s.nextDouble());
+ assertEquals(456.0, s.nextDouble());
+ assertEquals(123.4, s.nextDouble());
+ assertEquals(0.123, s.nextDouble());
+ try {
+ s.nextDouble();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(123.4, s.nextDouble());
+ assertEquals(-456.7, s.nextDouble());
+ assertEquals(123456.789, s.nextDouble());
+ try {
+ s.nextDouble();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // Scientific notation
+ s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(1.234E12, s.nextDouble());
+ assertEquals(-4.567E14, s.nextDouble());
+ assertEquals(1.23456789E-5, s.nextDouble());
+
+ s = new Scanner("NaN Infinity -Infinity");
+ assertEquals(Double.NaN, s.nextDouble());
+ assertEquals(Double.POSITIVE_INFINITY, s.nextDouble());
+ assertEquals(Double.NEGATIVE_INFINITY, s.nextDouble());
+
+ //The following test case fails on RI
+ s=new Scanner("\u221e");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(Double.POSITIVE_INFINITY, s.nextDouble());
+
+ String str=String.valueOf(Double.MAX_VALUE*2);
+ s=new Scanner(str);
+ assertEquals(Double.POSITIVE_INFINITY,s.nextDouble());
+
+ /*
+ * 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.ENGLISH);
+ assertEquals(23456.0, s.nextDouble());
+ s.useLocale(Locale.GERMANY);
+ assertEquals(23.456, s.nextDouble());
+
+ s = new Scanner("23.456 23.456");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(23.456, s.nextDouble());
+ s.useLocale(Locale.GERMANY);
+ assertEquals(23456.0, s.nextDouble());
+
+ s = new Scanner("23,456.7 23.456,7");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(23456.7, s.nextDouble());
+ s.useLocale(Locale.GERMANY);
+ assertEquals(23456.7, s.nextDouble());
+
+ s = new Scanner("-123.4");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(-123.4, s.nextDouble());
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#nextBigDecimal()
+ */
+ public void test_nextBigDecimal() throws IOException {
+ s = new Scanner("123 45\u0666. 123.4 .123 ");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(new BigDecimal("123"), s.nextBigDecimal());
+ assertEquals(new BigDecimal("456"), s.nextBigDecimal());
+ assertEquals(new BigDecimal("123.4"), s.nextBigDecimal());
+ assertEquals(new BigDecimal("0.123"), s.nextBigDecimal());
+ try {
+ s.nextBigDecimal();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(new BigDecimal("123.4"), s.nextBigDecimal());
+ assertEquals(new BigDecimal("-456.7"), s.nextBigDecimal());
+ assertEquals(new BigDecimal("123456.789"), s.nextBigDecimal());
+ try {
+ s.nextBigDecimal();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // Scientific notation
+ s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(new BigDecimal("1.234E12"), s.nextBigDecimal());
+ assertEquals(new BigDecimal("-4.567E14"), s.nextBigDecimal());
+ assertEquals(new BigDecimal("1.23456789E-5"), s.nextBigDecimal());
+
+ s = new Scanner("NaN");
+ try {
+ s.nextBigDecimal();
+ 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.ENGLISH);
+ assertEquals(new BigDecimal("23456"), s.nextBigDecimal());
+ s.useLocale(Locale.GERMANY);
+ assertEquals(new BigDecimal("23.456"), s.nextBigDecimal());
+
+ s = new Scanner("23.456 23.456");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(new BigDecimal("23.456"), s.nextBigDecimal());
+ s.useLocale(Locale.GERMANY);
+ assertEquals(new BigDecimal("23456"), s.nextBigDecimal());
+
+ s = new Scanner("23,456.7");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(new BigDecimal("23456.7"), s.nextBigDecimal());
+
+ s = new Scanner("-123.4");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(new BigDecimal("-123.4"), s.nextBigDecimal());
}
/**