Search in sources :

Example 46 with InputMismatchException

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

the class ScannerTest method test_hasNextByte.

public void test_hasNextByte() throws IOException {
    s = new Scanner("123 126");
    assertTrue(s.hasNextByte());
    assertEquals(123, s.nextByte());
    assertTrue(s.hasNextByte());
    assertEquals(126, s.nextByte());
    assertFalse(s.hasNextByte());
    try {
        s.nextByte();
        fail();
    } catch (NoSuchElementException expected) {
    }
    // If the radix is different from 10
    s = new Scanner("123 126");
    s.useRadix(5);
    assertTrue(s.hasNextByte());
    assertEquals(38, s.nextByte());
    assertFalse(s.hasNextByte());
    try {
        s.nextByte();
        fail();
    } catch (InputMismatchException expected) {
    }
    // If the number is out of range
    s = new Scanner("1234");
    assertFalse(s.hasNextByte());
    try {
        s.nextByte();
        fail();
    } catch (InputMismatchException expected) {
    }
    /*
         * The input string has Arabic-Indic digits.
         */
    s = new Scanner("1٠2 12٦");
    assertTrue(s.hasNextByte());
    assertEquals(102, s.nextByte());
    s.useRadix(5);
    assertFalse(s.hasNextByte());
    try {
        s.nextByte();
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useRadix(10);
    assertTrue(s.hasNextByte());
    assertEquals(126, s.nextByte());
    s = new Scanner("012");
    assertEquals(12, s.nextByte());
    s = new Scanner("E");
    s.useRadix(16);
    assertTrue(s.hasNextByte());
    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);
    assertTrue(s.hasNextByte());
    assertEquals(100, s.nextByte());
    s = new Scanner("1००");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextByte());
    assertEquals(100, s.nextByte());
    s = new Scanner("1๐๐");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextByte());
    assertEquals(100, s.nextByte());
    s = new Scanner("-123");
    s.useLocale(new Locale("ar", "AE"));
    assertTrue(s.hasNextByte());
    assertEquals(-123, s.nextByte());
    s = new Scanner("-123");
    s.useLocale(new Locale("mk", "MK"));
    assertTrue(s.hasNextByte());
    assertEquals(-123, s.nextByte());
}
Also used : Locale(java.util.Locale) Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 47 with InputMismatchException

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

the class ScannerTest method test_hasNextBigIntegerI.

/**
     * @throws IOException
     * @tests java.util.Scanner#hasNextBigInteger(int)
     */
public void test_hasNextBigIntegerI() throws IOException {
    s = new Scanner("123 456");
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("123"), s.nextBigInteger(10));
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("456"), s.nextBigInteger(10));
    assertFalse(s.hasNextBigInteger(10));
    try {
        s.nextBigInteger(10);
        fail();
    } catch (NoSuchElementException expected) {
    }
    // If the radix is different from 10
    s = new Scanner("123 456");
    assertTrue(s.hasNextBigInteger(5));
    assertEquals(new BigInteger("38"), s.nextBigInteger(5));
    assertFalse(s.hasNextBigInteger(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);
    assertFalse(s.hasNextBigInteger(10));
    try {
        s.nextBigInteger(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(Locale.ENGLISH);
    // If exception is thrown out, input will not be advanced.
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
    assertTrue(s.hasNextBigInteger(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);
    assertFalse(s.hasNextBigInteger(10));
    try {
        s.nextBigInteger(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(new Locale("it", "CH"));
    // If exception is thrown out, input will not be advanced.
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
    /*
         * The input string has Arabic-Indic digits.
         */
    s = new Scanner("1٠2 1٦2");
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("102"), s.nextBigInteger(10));
    assertFalse(s.hasNextBigInteger(5));
    try {
        s.nextBigInteger(5);
        fail();
    } catch (InputMismatchException expected) {
    }
    assertTrue(s.hasNextBigInteger(10));
    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);
    assertFalse(s.hasNextBigInteger(10));
    try {
        s.nextBigInteger(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s.useLocale(Locale.GERMANY);
    // If exception is thrown out, input will not be advanced.
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
    // The input string starts with zero
    s = new Scanner("03,456");
    s.useLocale(Locale.ENGLISH);
    assertFalse(s.hasNextBigInteger(10));
    try {
        s.nextBigInteger(10);
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("03456");
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
    s = new Scanner("٠3,456");
    s.useLocale(Locale.ENGLISH);
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
    s = new Scanner("E34");
    assertTrue(s.hasNextBigInteger(16));
    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);
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
    s = new Scanner("123००");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
    s = new Scanner("123๐๐");
    s.useLocale(Locale.CHINESE);
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
    s = new Scanner("-123");
    s.useLocale(new Locale("ar", "AE"));
    assertTrue(s.hasNextBigInteger(10));
    assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
    s = new Scanner("-123");
    s.useLocale(new Locale("mk", "MK"));
    assertTrue(s.hasNextBigInteger(10));
    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 48 with InputMismatchException

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

the class ScannerTest method test_nextLong.

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

Example 49 with InputMismatchException

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

the class ScannerTest method test_nextLongI.

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

Example 50 with InputMismatchException

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

the class ScannerTest method test_hasNextShort.

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