Search in sources :

Example 51 with InputMismatchException

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

the class ScannerTest method test_nextByte.

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

Example 52 with InputMismatchException

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

the class ScannerTest method test_nextBigInteger.

/**
     * @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();
    } catch (NoSuchElementException 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();
    } 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();
        fail();
    } catch (InputMismatchException 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();
    } 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());
    assertEquals(new BigInteger("23456"), s.nextBigInteger());
    /*
         * The input string has Arabic-Indic digits.
         */
    s = new Scanner("1٠2 1٦2");
    assertEquals(new BigInteger("102"), s.nextBigInteger());
    s.useRadix(5);
    try {
        s.nextBigInteger();
        fail();
    } catch (InputMismatchException 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٦ 23.456");
    s.useLocale(Locale.CHINESE);
    try {
        s.nextBigInteger();
        fail();
    } catch (InputMismatchException 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();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("03456");
    assertEquals(new BigInteger("3456"), s.nextBigInteger());
    s = new Scanner("٠3,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' '०' '๐'
         * respectively, but they are not differentiated.
         */
    s = new Scanner("12300");
    s.useLocale(Locale.CHINESE);
    assertEquals(new BigInteger("12300"), s.nextBigInteger());
    s = new Scanner("123००");
    s.useLocale(Locale.CHINESE);
    assertEquals(new BigInteger("12300"), s.nextBigInteger());
    s = new Scanner("123๐๐");
    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());
}
Also used : Locale(java.util.Locale) Scanner(java.util.Scanner) BigInteger(java.math.BigInteger) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 53 with InputMismatchException

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

the class ScannerTest method test_nextIntI.

/**
     * @throws IOException
     * @tests java.util.Scanner#nextInt(int)
     */
public void test_nextIntI() throws IOException {
    s = new Scanner("123 456");
    assertEquals(123, s.nextInt(10));
    assertEquals(456, s.nextInt(10));
    try {
        s.nextInt(10);
        fail();
    } catch (NoSuchElementException expected) {
    }
    // If the radix is different from 10
    s = new Scanner("123 456");
    assertEquals(38, s.nextInt(5));
    try {
        s.nextInt(5);
        fail();
    } catch (InputMismatchException expected) {
    }
    // If the number is out of range
    s = new Scanner("123456789123456789123456789123456789");
    try {
        s.nextInt(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.nextInt(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(Locale.ENGLISH);
    // If exception is thrown out, input will not be advanced.
    assertEquals(23456, s.nextInt(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);
    try {
        s.nextInt(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(new Locale("it", "CH"));
    // If exception is thrown out, input will not be advanced.
    assertEquals(23456, s.nextInt(10));
    assertEquals(23456, s.nextInt(10));
    /*
         * The input string has Arabic-Indic digits.
         */
    s = new Scanner("1٠2 1٦2");
    assertEquals(102, s.nextInt(10));
    try {
        s.nextInt(5);
        fail();
    } catch (InputMismatchException expected) {
    }
    assertEquals(162, s.nextInt(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.nextInt(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(Locale.GERMANY);
    // If exception is thrown out, input will not be advanced.
    assertEquals(23456, s.nextInt(10));
    assertEquals(23456, s.nextInt(10));
    // The input string starts with zero
    s = new Scanner("03,456");
    s.useLocale(Locale.ENGLISH);
    try {
        s.nextInt(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("03456");
    assertEquals(3456, s.nextInt(10));
    s = new Scanner("٠3,456");
    s.useLocale(Locale.ENGLISH);
    assertEquals(3456, s.nextInt(10));
    s = new Scanner("E3456");
    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);
    assertEquals(930902, s.nextInt(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.nextInt(10));
    s = new Scanner("123००");
    s.useLocale(Locale.CHINESE);
    assertEquals(12300, s.nextInt(10));
    s = new Scanner("123๐๐");
    s.useLocale(Locale.CHINESE);
    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 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(10));
    //assertEquals(-123, s.nextInt(10));
    try {
        s.nextInt(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("-123 123-");
    s.useLocale(new Locale("mk", "MK"));
    assertEquals(-123, s.nextInt(10));
    try {
        s.nextInt();
        fail();
    } catch (InputMismatchException expected) {
    }
    // Skip the un-recognizable token 123-.
    assertEquals("123-", s.next());
    // RI
    try {
        s.nextInt(Character.MIN_RADIX - 1);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        s.nextInt(Character.MAX_RADIX + 1);
        fail();
    } catch (IllegalArgumentException expected) {
    }
}
Also used : Locale(java.util.Locale) Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 54 with InputMismatchException

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

the class ScannerTest method test_hasNextDouble.

/**
     * @throws IOException
     * @tests java.util.Scanner#nextDouble()
     */
public void test_hasNextDouble() throws IOException {
    s = new Scanner("123 45٦. 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();
    } catch (NoSuchElementException expected) {
    }
    s = new Scanner("+123.4 -456.7 123,456.789 0.1٢3,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();
    } catch (InputMismatchException 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());
    s = new Scanner("+123.4 -456.7");
    s.useLocale(Locale.ENGLISH);
    assertTrue(s.hasNextDouble());
    s.close();
    try {
        s.nextDouble();
        fail();
    } catch (IllegalStateException expected) {
    }
}
Also used : Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 55 with InputMismatchException

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

the class ScannerTest method test_hasNextFloat.

/**
     * @throws IOException
     * @tests java.util.Scanner#hasNextFloat()
     */
public void test_hasNextFloat() throws IOException {
    s = new Scanner("123 45٦. 123.4 .123 ");
    s.useLocale(Locale.ENGLISH);
    assertTrue(s.hasNextFloat());
    assertEquals((float) 123.0, s.nextFloat());
    assertTrue(s.hasNextFloat());
    assertEquals((float) 456.0, s.nextFloat());
    assertTrue(s.hasNextFloat());
    assertEquals((float) 123.4, s.nextFloat());
    assertTrue(s.hasNextFloat());
    assertEquals((float) 0.123, s.nextFloat());
    assertFalse(s.hasNextFloat());
    try {
        s.nextFloat();
        fail();
    } catch (NoSuchElementException expected) {
    }
    s = new Scanner("+123.4 -456.7 123,456.789 0.1٢3,4");
    s.useLocale(Locale.ENGLISH);
    assertTrue(s.hasNextFloat());
    assertEquals((float) 123.4, s.nextFloat());
    assertTrue(s.hasNextFloat());
    assertEquals((float) -456.7, s.nextFloat());
    assertTrue(s.hasNextFloat());
    assertEquals((float) 123456.789, s.nextFloat());
    assertFalse(s.hasNextFloat());
    try {
        s.nextFloat();
        fail();
    } catch (InputMismatchException expected) {
    }
    // Scientific notation
    s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
    s.useLocale(Locale.ENGLISH);
    assertTrue(s.hasNextFloat());
    assertEquals((float) 1.234E12, s.nextFloat());
    assertTrue(s.hasNextFloat());
    assertEquals((float) -4.567E14, s.nextFloat());
    assertTrue(s.hasNextFloat());
    assertEquals((float) 1.23456789E-5, s.nextFloat());
    s = new Scanner("NaN Infinity -Infinity");
    assertTrue(s.hasNextFloat());
    assertEquals(Float.NaN, s.nextFloat());
    assertTrue(s.hasNextFloat());
    assertEquals(Float.POSITIVE_INFINITY, s.nextFloat());
    assertTrue(s.hasNextFloat());
    assertEquals(Float.NEGATIVE_INFINITY, s.nextFloat());
    String str = String.valueOf(Float.MAX_VALUE * 2);
    s = new Scanner(str);
    assertTrue(s.hasNextFloat());
    assertEquals(Float.POSITIVE_INFINITY, s.nextFloat());
    /*
         * 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.hasNextFloat());
    assertEquals((float) 23456.0, s.nextFloat());
    s.useLocale(Locale.GERMANY);
    assertTrue(s.hasNextFloat());
    assertEquals((float) 23.456, s.nextFloat());
    s = new Scanner("23.456 23.456");
    s.useLocale(Locale.ENGLISH);
    assertTrue(s.hasNextFloat());
    assertEquals((float) 23.456, s.nextFloat());
    s.useLocale(Locale.GERMANY);
    assertTrue(s.hasNextFloat());
    assertEquals((float) 23456.0, s.nextFloat());
    s = new Scanner("23,456.7 23.456,7");
    s.useLocale(Locale.ENGLISH);
    assertTrue(s.hasNextFloat());
    assertEquals((float) 23456.7, s.nextFloat());
    s.useLocale(Locale.GERMANY);
    assertTrue(s.hasNextFloat());
    assertEquals((float) 23456.7, s.nextFloat());
    //FIXME
    //        s = new Scanner("-123.4 123.4- -123.4-");
    //        s.useLocale(new Locale("ar", "AE"));
    //        assertTrue(s.hasNextFloat());
    //        assertEquals((float)-123.4, s.nextFloat());
    //        //The following test case fails on RI
    //        assertTrue(s.hasNextFloat());
    //        assertEquals((float)-123.4, s.nextFloat());
    //        try {
    //            s.nextFloat();
    //            fail();
    //        } catch (InputMismatchException expected) {
    //        }
    s = new Scanner("123- -123");
    s.useLocale(new Locale("mk", "MK"));
    assertFalse(s.hasNextFloat());
    try {
        s.nextFloat();
        fail();
    } catch (InputMismatchException expected) {
    }
    // Skip the un-recognizable token 123-.
    assertEquals("123-", s.next());
    assertTrue(s.hasNextFloat());
    assertEquals((float) -123.0, s.nextFloat());
    s = new Scanner("+123.4 -456.7");
    s.useLocale(Locale.ENGLISH);
    assertTrue(s.hasNextFloat());
    s.close();
    try {
        s.nextFloat();
        fail();
    } catch (IllegalStateException expected) {
    }
}
Also used : Locale(java.util.Locale) Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

InputMismatchException (java.util.InputMismatchException)83 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