Search in sources :

Example 91 with NoSuchElementException

use of java.util.NoSuchElementException in project robovm by robovm.

the class ScannerTest method test_nextBigIntegerI.

/**
     * @throws IOException
     * @tests java.util.Scanner#nextBigInteger(int)
     */
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.nextBigInteger(10);
        fail();
    } catch (NoSuchElementException expected) {
    }
    // If the radix is different from 10
    s = new Scanner("123 456");
    assertEquals(new BigInteger("38"), s.nextBigInteger(5));
    try {
        s.nextBigInteger(5);
        fail();
    } catch (InputMismatchException 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(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    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.nextBigInteger(10);
        fail();
    } catch (InputMismatchException 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));
    /*
         * The input string has Arabic-Indic digits.
         */
    s = new Scanner("1٠2 1٦2");
    assertEquals(new BigInteger("102"), s.nextBigInteger(10));
    try {
        s.nextBigInteger(5);
        fail();
    } catch (InputMismatchException expected) {
    }
    assertEquals(new BigInteger("162"), s.nextBigInteger(10));
    /*
         * '.' is used in many locales as group separator. The input string
         * has Arabic-Indic digits .
         */
    s = new Scanner("23.45٦ 23.456");
    s.useLocale(Locale.CHINESE);
    try {
        s.nextBigInteger(10);
        fail();
    } catch (InputMismatchException 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));
    // The input string starts with zero
    s = new Scanner("03,456");
    s.useLocale(Locale.ENGLISH);
    try {
        s.nextBigInteger(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("03456");
    assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
    s = new Scanner("٠3,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' '०' '๐'
         * 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००");
    s.useLocale(Locale.CHINESE);
    assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
    s = new Scanner("123๐๐");
    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));
}
Also used : Locale(java.util.Locale) Scanner(java.util.Scanner) BigInteger(java.math.BigInteger) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 92 with NoSuchElementException

use of java.util.NoSuchElementException in project robovm by robovm.

the class ScannerTest method test_nextShortI.

/**
     * @throws IOException
     * @tests java.util.Scanner#nextShort(int)
     */
public void test_nextShortI() throws IOException {
    s = new Scanner("123 456");
    assertEquals(123, s.nextShort(10));
    assertEquals(456, s.nextShort(10));
    try {
        s.nextShort(10);
        fail();
    } catch (NoSuchElementException expected) {
    }
    // If the radix is different from 10
    s = new Scanner("123 456");
    assertEquals(38, s.nextShort(5));
    try {
        s.nextShort(5);
        fail();
    } catch (InputMismatchException expected) {
    }
    // If the number is out of range
    s = new Scanner("123456789");
    try {
        s.nextShort(10);
        fail();
    } catch (InputMismatchException 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.nextShort(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(Locale.ENGLISH);
    // If exception is thrown out, input will not be advanced.
    assertEquals(23456, s.nextShort(10));
    assertEquals(23456, s.nextShort(10));
    /*
         * ''' is used in many locales as group separator.
         */
    s = new Scanner("23'456 23'456");
    s.useLocale(Locale.GERMANY);
    try {
        s.nextShort(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(new Locale("it", "CH"));
    // If exception is thrown out, input will not be advanced.
    assertEquals(23456, s.nextShort(10));
    assertEquals(23456, s.nextShort(10));
    /*
         * The input string has Arabic-Indic digits.
         */
    s = new Scanner("1٠2 1٦2");
    assertEquals(102, s.nextShort(10));
    try {
        s.nextShort(5);
        fail();
    } catch (InputMismatchException expected) {
    }
    assertEquals(162, s.nextShort(10));
    /*
         * '.' is used in many locales as group separator. The input string
         * has Arabic-Indic digits .
         */
    s = new Scanner("23.45٦ 23.456");
    s.useLocale(Locale.CHINESE);
    try {
        s.nextShort(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(Locale.GERMANY);
    // If exception is thrown out, input will not be advanced.
    assertEquals(23456, s.nextShort(10));
    assertEquals(23456, s.nextShort(10));
    // The input string starts with zero
    s = new Scanner("03,456");
    s.useLocale(Locale.ENGLISH);
    try {
        s.nextShort(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("03456");
    assertEquals(3456, s.nextShort(10));
    s = new Scanner("٠3,456");
    s.useLocale(Locale.ENGLISH);
    assertEquals(3456, s.nextShort(10));
    s = new Scanner("E34");
    assertEquals(3636, s.nextShort(16));
    /*
         * There are 3 types of zero digit in all locales, '0' '०' '๐'
         * respectively, but they are not differentiated.
         */
    s = new Scanner("12300");
    s.useLocale(Locale.CHINESE);
    assertEquals(12300, s.nextShort(10));
    s = new Scanner("123००");
    s.useLocale(Locale.CHINESE);
    assertEquals(12300, s.nextShort(10));
    s = new Scanner("123๐๐");
    s.useLocale(Locale.CHINESE);
    assertEquals(12300, s.nextShort(10));
    s = new Scanner("-123");
    s.useLocale(new Locale("ar", "AE"));
    assertEquals(-123, s.nextShort(10));
    s = new Scanner("-123");
    s.useLocale(new Locale("mk", "MK"));
    assertEquals(-123, s.nextShort(10));
}
Also used : Locale(java.util.Locale) Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 93 with NoSuchElementException

use of java.util.NoSuchElementException in project robovm by robovm.

the class ScannerTest method test_hasNextBigDecimal.

/**
     * @throws IOException
     * @tests java.util.Scanner#hasNextBigDecimal()
     */
public void test_hasNextBigDecimal() throws IOException {
    s = new Scanner("123 45٦. 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();
    } catch (NoSuchElementException expected) {
    }
    s = new Scanner("+123.4 -456.7 123,456.789 0.1٢3,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();
    } catch (InputMismatchException 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();
    } catch (InputMismatchException 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());
}
Also used : Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) BigDecimal(java.math.BigDecimal) NoSuchElementException(java.util.NoSuchElementException)

Example 94 with NoSuchElementException

use of java.util.NoSuchElementException in project robovm by robovm.

the class ScannerTest method test_hasNextIntI.

/**
     * @throws IOException
     * @tests java.util.Scanner#hasNextInt(int)
     */
public void test_hasNextIntI() throws IOException {
    s = new Scanner("123 456");
    assertEquals(123, s.nextInt(10));
    assertTrue(s.hasNextInt(10));
    assertEquals(456, s.nextInt(10));
    assertFalse(s.hasNextInt(10));
    try {
        s.nextInt(10);
        fail();
    } catch (NoSuchElementException expected) {
    }
    // If the radix is different from 10
    s = new Scanner("123 456");
    assertTrue(s.hasNextInt(5));
    assertEquals(38, s.nextInt(5));
    assertFalse(s.hasNextInt(5));
    try {
        s.nextInt(5);
        fail();
    } catch (InputMismatchException expected) {
    }
    // If the number is out of range
    s = new Scanner("123456789123456789123456789123456789");
    assertFalse(s.hasNextInt(10));
    /*
         * Different locale can only recognize corresponding locale sensitive
         * string. ',' is used in many locales as group separator.
         */
    s = new Scanner("23,456");
    s.useLocale(Locale.GERMANY);
    assertFalse(s.hasNextInt(10));
    s.useLocale(Locale.ENGLISH);
    // If exception is thrown out, input will not be advanced.
    assertTrue(s.hasNextInt(10));
    /*
         * ''' is used in many locales as group separator.
         */
    s = new Scanner("23'456");
    s.useLocale(Locale.GERMANY);
    assertFalse(s.hasNextInt(10));
    s.useLocale(new Locale("it", "CH"));
    // If exception is thrown out, input will not be advanced.
    assertTrue(s.hasNextInt(10));
    /*
         * The input string has Arabic-Indic digits.
         */
    s = new Scanner("1٦2");
    assertTrue(s.hasNextInt(10));
    assertFalse(s.hasNextInt(5));
    /*
         * '.' is used in many locales as group separator. The input string
         * has Arabic-Indic digits .
         */
    s = new Scanner("23.45٦");
    s.useLocale(Locale.CHINESE);
    assertFalse(s.hasNextInt(10));
    try {
        s.nextInt(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(Locale.GERMANY);
    assertTrue(s.hasNextInt(10));
    // The input string starts with zero
    s = new Scanner("03,456");
    s.useLocale(Locale.ENGLISH);
    assertFalse(s.hasNextInt(10));
    try {
        s.nextInt(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("03456");
    assertTrue(s.hasNextInt(10));
    assertEquals(3456, s.nextInt(10));
    s = new Scanner("٠3,456");
    s.useLocale(Locale.ENGLISH);
    assertTrue(s.hasNextInt(10));
    assertEquals(3456, s.nextInt(10));
    s = new Scanner("E3456");
    assertTrue(s.hasNextInt(16));
    assertEquals(930902, s.nextInt(16));
    // The following test case fails on RI, because RI does not support
    // letter as leading digit
    s = new Scanner("E3,456");
    s.useLocale(Locale.ENGLISH);
    assertTrue(s.hasNextInt(16));
    assertEquals(930902, s.nextInt(16));
    // If parameter radix is illegal, the following test case fails on RI
    try {
        s.hasNextInt(Character.MIN_RADIX - 1);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    /*
         * There are 3 types of zero digit in all locales, '0' '०' '๐'
         * respectively, but they are not differentiated.
         */
    s = new Scanner("12300");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextInt(10));
    assertEquals(12300, s.nextInt(10));
    s = new Scanner("123००");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextInt(10));
    assertEquals(12300, s.nextInt(10));
    s = new Scanner("123๐๐");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextInt(10));
    assertEquals(12300, s.nextInt(10));
    /*
         * There are three types of negative prefix all in all. '' '-' '(' There
         * are three types of negative suffix all in all. '' '-' ')' '(' and ')'
         * must be used together. Prefix '-' and suffix '-' must be used
         * exclusively.
         */
    /*
         * According to Integer regular expression: Integer :: = ( [-+]? (*
         * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix |
         * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be
         * recognized by scanner with locale ar_AE, (123) should be recognized
         * by scanner with locale mk_MK. But this is not the case on RI.
         */
    s = new Scanner("-123 123- -123-");
    s.useLocale(new Locale("ar", "AE"));
    assertTrue(s.hasNextInt(10));
    assertEquals(-123, s.nextInt(10));
    // The following test case fails on RI
    assertTrue(s.hasNextInt(10));
    assertEquals(-123, s.nextInt(10));
    assertFalse(s.hasNextInt(10));
    try {
        s.nextInt(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("-123 123-");
    s.useLocale(new Locale("mk", "MK"));
    assertTrue(s.hasNextInt(10));
    assertEquals(-123, s.nextInt(10));
    assertFalse(s.hasNextInt(10));
    try {
        s.nextInt();
        fail();
    } catch (InputMismatchException expected) {
    }
    // Skip the un-recognizable token 123-.
    assertEquals("123-", s.next());
}
Also used : Locale(java.util.Locale) Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 95 with NoSuchElementException

use of java.util.NoSuchElementException in project robovm by robovm.

the class ScannerTest method test_hasNextShortI.

/**
     * @throws IOException
     * @tests java.util.Scanner#hasNextShort(int)
     */
public void test_hasNextShortI() throws IOException {
    s = new Scanner("123 456");
    assertTrue(s.hasNextShort(10));
    assertEquals(123, s.nextShort(10));
    assertTrue(s.hasNextShort(10));
    assertEquals(456, s.nextShort(10));
    assertFalse(s.hasNextShort(10));
    try {
        s.nextShort(10);
        fail();
    } catch (NoSuchElementException expected) {
    }
    // If the radix is different from 10
    s = new Scanner("123 456");
    assertTrue(s.hasNextShort(5));
    assertEquals(38, s.nextShort(5));
    assertFalse(s.hasNextShort(5));
    try {
        s.nextShort(5);
        fail();
    } catch (InputMismatchException expected) {
    }
    // If the number is out of range
    s = new Scanner("123456789");
    assertFalse(s.hasNextShort(10));
    try {
        s.nextShort(10);
        fail();
    } catch (InputMismatchException 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);
    assertFalse(s.hasNextShort(10));
    try {
        s.nextShort(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(Locale.ENGLISH);
    // If exception is thrown out, input will not be advanced.
    assertTrue(s.hasNextShort(10));
    assertEquals(23456, s.nextInt(10));
    assertTrue(s.hasNextShort(10));
    assertEquals(23456, s.nextInt(10));
    /*
         * ''' is used in many locales as group separator.
         */
    s = new Scanner("23'456 23'456");
    s.useLocale(Locale.GERMANY);
    assertFalse(s.hasNextShort(10));
    try {
        s.nextShort(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(new Locale("it", "CH"));
    // If exception is thrown out, input will not be advanced.
    assertTrue(s.hasNextShort(10));
    assertEquals(23456, s.nextShort(10));
    assertTrue(s.hasNextShort(10));
    assertEquals(23456, s.nextShort(10));
    /*
         * The input string has Arabic-Indic digits.
         */
    s = new Scanner("1٠2 1٦2");
    assertTrue(s.hasNextShort(10));
    assertEquals(102, s.nextShort(10));
    assertFalse(s.hasNextShort(5));
    try {
        s.nextShort(5);
        fail();
    } catch (InputMismatchException expected) {
    }
    assertTrue(s.hasNextShort(10));
    assertEquals(162, s.nextShort(10));
    /*
         * '.' is used in many locales as group separator. The input string
         * has Arabic-Indic digits .
         */
    s = new Scanner("23.45٦ 23.456");
    s.useLocale(Locale.CHINESE);
    assertFalse(s.hasNextShort(10));
    try {
        s.nextShort(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(Locale.GERMANY);
    // If exception is thrown out, input will not be advanced.
    assertTrue(s.hasNextShort(10));
    assertEquals(23456, s.nextShort(10));
    assertTrue(s.hasNextShort(10));
    assertEquals(23456, s.nextShort(10));
    // The input string starts with zero
    s = new Scanner("03,456");
    s.useLocale(Locale.ENGLISH);
    assertFalse(s.hasNextShort(10));
    try {
        s.nextShort(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("03456");
    assertTrue(s.hasNextShort(10));
    assertEquals(3456, s.nextShort(10));
    s = new Scanner("٠3,456");
    s.useLocale(Locale.ENGLISH);
    assertTrue(s.hasNextShort(10));
    assertEquals(3456, s.nextShort(10));
    s = new Scanner("E34");
    assertTrue(s.hasNextShort(16));
    assertEquals(3636, s.nextShort(16));
    /*
         * There are 3 types of zero digit in all locales, '0' '०' '๐'
         * respectively, but they are not differentiated.
         */
    s = new Scanner("12300");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextShort(10));
    assertEquals(12300, s.nextShort(10));
    s = new Scanner("123००");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextShort(10));
    assertEquals(12300, s.nextShort(10));
    s = new Scanner("123๐๐");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextShort(10));
    assertEquals(12300, s.nextShort(10));
    s = new Scanner("-123");
    s.useLocale(new Locale("ar", "AE"));
    assertTrue(s.hasNextShort(10));
    assertEquals(-123, s.nextShort(10));
    s = new Scanner("-123");
    s.useLocale(new Locale("mk", "MK"));
    assertTrue(s.hasNextShort(10));
    assertEquals(-123, s.nextShort(10));
}
Also used : Locale(java.util.Locale) Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

NoSuchElementException (java.util.NoSuchElementException)1629 Iterator (java.util.Iterator)234 ArrayList (java.util.ArrayList)138 IOException (java.io.IOException)137 Test (org.junit.Test)95 StringTokenizer (java.util.StringTokenizer)88 Scanner (java.util.Scanner)86 Map (java.util.Map)56 List (java.util.List)54 File (java.io.File)51 HashMap (java.util.HashMap)47 InputMismatchException (java.util.InputMismatchException)47 LinkedList (java.util.LinkedList)32 HashSet (java.util.HashSet)31 Set (java.util.Set)29 CorruptDataException (com.ibm.j9ddr.CorruptDataException)28 Locale (java.util.Locale)27 Collection (java.util.Collection)26 BufferedReader (java.io.BufferedReader)24 Enumeration (java.util.Enumeration)24