Search in sources :

Example 16 with InputMismatchException

use of java.util.InputMismatchException 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 17 with InputMismatchException

use of java.util.InputMismatchException 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 18 with InputMismatchException

use of java.util.InputMismatchException 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)

Example 19 with InputMismatchException

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

the class ScannerTest method test_nextInt.

/**
     * @throws IOException
     * @tests java.util.Scanner#nextInt()
     */
public void test_nextInt() throws IOException {
    s = new Scanner("123 456");
    assertEquals(123, s.nextInt());
    assertEquals(456, s.nextInt());
    try {
        s.nextInt();
        fail();
    } catch (NoSuchElementException expected) {
    }
    // If the radix is different from 10
    s = new Scanner("123 456");
    s.useRadix(5);
    assertEquals(38, s.nextInt());
    try {
        s.nextInt();
        fail();
    } catch (InputMismatchException expected) {
    }
    // If the number is out of range
    s = new Scanner("123456789123456789123456789123456789");
    try {
        s.nextInt();
        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.nextInt();
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(Locale.ENGLISH);
    // If exception is thrown out, input will not be advanced.
    assertEquals(23456, s.nextInt());
    assertEquals(23456, s.nextInt());
    /*
         * ''' is used in many locales as group separator.
         */
    s = new Scanner("23'456 23'456");
    s.useLocale(Locale.GERMANY);
    try {
        s.nextInt();
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(new Locale("it", "CH"));
    // If exception is thrown out, input will not be advanced.
    assertEquals(23456, s.nextInt());
    assertEquals(23456, s.nextInt());
    /*
         * The input string has Arabic-Indic digits.
         */
    s = new Scanner("1٠2 1٦2");
    assertEquals(102, s.nextInt());
    s.useRadix(5);
    try {
        s.nextInt();
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useRadix(10);
    assertEquals(162, s.nextInt());
    /*
         * '.' 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.nextInt();
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(Locale.GERMANY);
    // If exception is thrown out, input will not be advanced.
    assertEquals(23456, s.nextInt());
    assertEquals(23456, s.nextInt());
    // The input string starts with zero
    s = new Scanner("03,456");
    s.useLocale(Locale.ENGLISH);
    try {
        s.nextInt();
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("03456");
    assertEquals(3456, s.nextInt());
    s = new Scanner("٠3,456");
    s.useLocale(Locale.ENGLISH);
    assertEquals(3456, s.nextInt());
    s = new Scanner("E3456");
    s.useRadix(16);
    assertEquals(930902, s.nextInt());
    // 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);
    s.useRadix(16);
    assertEquals(930902, s.nextInt());
    /*
         * 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.nextInt());
    s = new Scanner("123००");
    s.useLocale(Locale.CHINESE);
    assertEquals(12300, s.nextInt());
    s = new Scanner("123๐๐");
    s.useLocale(Locale.CHINESE);
    assertEquals(12300, s.nextInt());
    /*
         * There are three types of negative prefix all in all. '' '-' '(' There
         * are three types of negative suffix all in all. '' '-' ')' '(' and ')'
         * must be used togethor. 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) shouble 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"));
    assertEquals(-123, s.nextInt());
    //assertEquals(-123, s.nextInt());
    try {
        s.nextInt();
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("-123 123-");
    s.useLocale(new Locale("mk", "MK"));
    assertEquals(-123, s.nextInt());
    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 20 with InputMismatchException

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

the class ScannerTest method test_hasNextByteI.

/**
     * @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();
    } catch (NoSuchElementException 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();
    } catch (InputMismatchException expected) {
    }
    // If the number is out of range
    s = new Scanner("1234");
    assertFalse(s.hasNextByte(10));
    try {
        s.nextByte(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    /*
         * The input string has Arabic-Indic digits.
         */
    s = new Scanner("1٠2 12٦");
    assertTrue(s.hasNextByte(10));
    assertEquals(102, s.nextByte(10));
    assertFalse(s.hasNextByte(5));
    try {
        s.nextByte(5);
        fail();
    } catch (InputMismatchException 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' '०' '๐'
         * 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००");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextByte(10));
    assertEquals(100, s.nextByte(10));
    s = new Scanner("1๐๐");
    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));
}
Also used : Locale(java.util.Locale) Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

InputMismatchException (java.util.InputMismatchException)82 Scanner (java.util.Scanner)56 NoSuchElementException (java.util.NoSuchElementException)47 Locale (java.util.Locale)23 BufferUnderflowException (java.nio.BufferUnderflowException)5 Pattern (java.util.regex.Pattern)5 BufferedReader (java.io.BufferedReader)4 BigInteger (java.math.BigInteger)4 UnicodeReader (gdsc.core.utils.UnicodeReader)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 TodoList (de.djuelg.neuronizer.domain.model.preview.TodoList)2 TodoListHeader (de.djuelg.neuronizer.domain.model.todolist.TodoListHeader)2 Point (java.awt.Point)2 Serializable (java.io.Serializable)2 BigDecimal (java.math.BigDecimal)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1