Search in sources :

Example 41 with InputMismatchException

use of java.util.InputMismatchException in project j2objc by google.

the class InputMismatchExceptionTest method test_ConstructorLjava_lang_String.

/**
     * @tests java.util.InputMismatchException#InputMismatchException(String)
     */
public void test_ConstructorLjava_lang_String() {
    InputMismatchException exception = new InputMismatchException(ERROR_MESSAGE);
    assertNotNull(exception);
    assertEquals(ERROR_MESSAGE, exception.getMessage());
}
Also used : InputMismatchException(java.util.InputMismatchException)

Example 42 with InputMismatchException

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

the class ScannerTest method test_hasNextLongI.

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

Example 43 with InputMismatchException

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

the class ScannerTest method test_nextLPattern.

/**
     * @throws IOException
     * @tests java.util.Scanner#next(Pattern)
     */
public void test_nextLPattern() throws IOException {
    Pattern pattern;
    s = new Scanner("aab*2*").useDelimiter("\\*");
    pattern = Pattern.compile("a*b");
    assertEquals("aab", s.next(pattern));
    try {
        s.next(pattern);
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("word ? ");
    pattern = Pattern.compile("\\w+");
    assertEquals("word", s.next(pattern));
    try {
        s.next(pattern);
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("word1 word2  ");
    pattern = Pattern.compile("\\w+");
    assertEquals("word1", s.next(pattern));
    assertEquals("word2", s.next(pattern));
    // test boundary case
    try {
        s.next(pattern);
        fail();
    } catch (NoSuchElementException expected) {
    }
    // test socket inputStream
    os.write("aab 2".getBytes());
    serverSocket.close();
    s = new Scanner(client);
    pattern = Pattern.compile("a*b");
    assertEquals("aab", s.next(pattern));
    try {
        s.next(pattern);
        fail();
    } catch (InputMismatchException expected) {
    }
}
Also used : Pattern(java.util.regex.Pattern) Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 44 with InputMismatchException

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

the class ScannerTest method test_nextDouble.

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

Example 45 with InputMismatchException

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

the class ScannerTest method test_hasNextLong.

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