Search in sources :

Example 86 with NoSuchElementException

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

the class ScannerTest method test_nextBoolean.

/**
     * @throws IOException
     * @tests java.util.Scanner#nextBoolean()
     */
public void test_nextBoolean() throws IOException {
    // case insensitive
    s = new Scanner("TRue");
    assertTrue(s.nextBoolean());
    s = new Scanner("tRue false");
    assertTrue(s.nextBoolean());
    assertFalse(s.nextBoolean());
    try {
        s.nextBoolean();
        fail();
    } catch (NoSuchElementException expected) {
    }
    s = new Scanner("true1");
    try {
        s.nextBoolean();
        fail();
    } catch (InputMismatchException expected) {
    }
    try {
        s = new Scanner("");
        s.nextBoolean();
        fail();
    } catch (NoSuchElementException expected) {
    }
    // test socket inputStream
    os.write("true false".getBytes());
    serverSocket.close();
    s = new Scanner(client);
    assertTrue(s.nextBoolean());
    assertFalse(s.nextBoolean());
    // ues '*' as delimiter
    s = new Scanner("true**false").useDelimiter("\\*");
    assertTrue(s.nextBoolean());
    try {
        s.nextBoolean();
        fail();
    } catch (NoSuchElementException expected) {
    }
    s = new Scanner("false( )").useDelimiter("\\( \\)");
    assertFalse(s.nextBoolean());
}
Also used : Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 87 with NoSuchElementException

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

the class ScannerTest method test_nextByteI.

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

Example 88 with NoSuchElementException

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

the class ScannerTest method test_match.

/**
     * @tests java.util.Scanner#match()
     */
public void test_match() {
    MatchResult result;
    s = new Scanner("1 2 ");
    try {
        s.match();
        fail();
    } catch (IllegalStateException expected) {
    }
    assertEquals("1", s.next());
    assertEquals("2", s.next());
    result = s.match();
    assertEquals(2, result.start());
    assertEquals(3, result.end());
    assertEquals(2, result.start(0));
    assertEquals(3, result.end(0));
    assertEquals("2", result.group());
    assertEquals("2", result.group(0));
    assertEquals(0, result.groupCount());
    try {
        result.start(1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        s.next();
        fail();
    } catch (NoSuchElementException expected) {
    }
    try {
        s.match();
        fail();
    } catch (IllegalStateException expected) {
    }
    s = new Scanner("True faLse");
    try {
        s.match();
        fail();
    } catch (IllegalStateException expected) {
    }
    assertTrue(s.nextBoolean());
    result = s.match();
    assertEquals(0, result.start());
    assertEquals(4, result.end());
    assertEquals(0, result.start(0));
    assertEquals(4, result.end(0));
    assertEquals("True", result.group());
    assertEquals(0, result.groupCount());
    assertFalse(s.nextBoolean());
    try {
        s.nextBoolean();
        fail();
    } catch (NoSuchElementException expected) {
    }
    try {
        s.match();
        fail();
    } catch (IllegalStateException expected) {
    }
    s = new Scanner("True faLse");
    assertTrue(s.nextBoolean());
    result = s.match();
    assertEquals(0, result.start());
    assertEquals(4, result.end());
    assertEquals(0, result.start(0));
    assertEquals(4, result.end(0));
    assertEquals("True", result.group());
    assertEquals(0, result.groupCount());
    s.close();
    try {
        s.nextBoolean();
        fail();
    } catch (IllegalStateException expected) {
    }
    result = s.match();
    assertEquals(0, result.start());
    assertEquals(4, result.end());
    assertEquals(0, result.start(0));
    assertEquals(4, result.end(0));
    assertEquals("True", result.group());
    assertEquals(0, result.groupCount());
    s = new Scanner("True fase");
    assertTrue(s.nextBoolean());
    assertEquals(0, result.groupCount());
    try {
        s.nextBoolean();
        fail();
    } catch (InputMismatchException expected) {
    }
    try {
        s.match();
        fail();
    } catch (IllegalStateException expected) {
    }
    s = new Scanner("True fase");
    assertTrue(s.nextBoolean());
    try {
        s.next((Pattern) null);
        fail();
    } catch (NullPointerException expected) {
    }
    result = s.match();
    assertEquals(0, result.start());
    assertEquals(4, result.end());
    assertEquals(0, result.start(0));
    assertEquals(4, result.end(0));
    assertEquals("True", result.group());
    assertEquals(0, result.groupCount());
}
Also used : Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) MatchResult(java.util.regex.MatchResult) NoSuchElementException(java.util.NoSuchElementException)

Example 89 with NoSuchElementException

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

the class ScannerTest method test_nextFloat.

/**
     * @throws IOException
     * @tests java.util.Scanner#nextFloat()
     */
public void test_nextFloat() throws IOException {
    s = new Scanner("123 45٦. 123.4 .123 ");
    s.useLocale(Locale.ENGLISH);
    assertEquals((float) 123.0, s.nextFloat());
    assertEquals((float) 456.0, s.nextFloat());
    assertEquals((float) 123.4, s.nextFloat());
    assertEquals((float) 0.123, s.nextFloat());
    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);
    assertEquals((float) 123.4, s.nextFloat());
    assertEquals((float) -456.7, s.nextFloat());
    assertEquals((float) 123456.789, s.nextFloat());
    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);
    assertEquals((float) 1.234E12, s.nextFloat());
    assertEquals((float) -4.567E14, s.nextFloat());
    assertEquals((float) 1.23456789E-5, s.nextFloat());
    s = new Scanner("NaN Infinity -Infinity");
    assertEquals(Float.NaN, s.nextFloat());
    assertEquals(Float.POSITIVE_INFINITY, s.nextFloat());
    assertEquals(Float.NEGATIVE_INFINITY, s.nextFloat());
    String str = String.valueOf(Float.MAX_VALUE * 2);
    s = new Scanner(str);
    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);
    assertEquals((float) 23456.0, s.nextFloat());
    s.useLocale(Locale.GERMANY);
    assertEquals((float) 23.456, s.nextFloat());
    s = new Scanner("23.456 23.456");
    s.useLocale(Locale.ENGLISH);
    assertEquals((float) 23.456, s.nextFloat());
    s.useLocale(Locale.GERMANY);
    assertEquals((float) 23456.0, s.nextFloat());
    s = new Scanner("23,456.7 23.456,7");
    s.useLocale(Locale.ENGLISH);
    assertEquals((float) 23456.7, s.nextFloat());
    s.useLocale(Locale.GERMANY);
    assertEquals((float) 23456.7, s.nextFloat());
    s = new Scanner("-123.4 123.4- -123.4-");
    s.useLocale(new Locale("ar", "AE"));
    //        assertEquals((float)-123.4, s.nextFloat());
    try {
        s.nextFloat();
        fail();
    } catch (InputMismatchException expected) {
    }
    s = new Scanner("123- -123");
    s.useLocale(new Locale("mk", "MK"));
    try {
        s.nextFloat();
        fail();
    } catch (InputMismatchException expected) {
    }
    // Skip the un-recognizable token 123-.
    assertEquals("123-", s.next());
    assertEquals((float) -123.0, s.nextFloat());
}
Also used : Locale(java.util.Locale) Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 90 with NoSuchElementException

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

the class ScannerTest method test_nextBigDecimal.

/**
     * @throws IOException
     * @tests java.util.Scanner#nextBigDecimal()
     */
public void test_nextBigDecimal() throws IOException {
    s = new Scanner("123 45٦. 123.4 .123 ");
    s.useLocale(Locale.ENGLISH);
    assertEquals(new BigDecimal("123"), s.nextBigDecimal());
    assertEquals(new BigDecimal("456"), s.nextBigDecimal());
    assertEquals(new BigDecimal("123.4"), s.nextBigDecimal());
    assertEquals(new BigDecimal("0.123"), s.nextBigDecimal());
    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);
    assertEquals(new BigDecimal("123.4"), s.nextBigDecimal());
    assertEquals(new BigDecimal("-456.7"), s.nextBigDecimal());
    assertEquals(new BigDecimal("123456.789"), s.nextBigDecimal());
    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);
    assertEquals(new BigDecimal("1.234E12"), s.nextBigDecimal());
    assertEquals(new BigDecimal("-4.567E14"), s.nextBigDecimal());
    assertEquals(new BigDecimal("1.23456789E-5"), s.nextBigDecimal());
    s = new Scanner("NaN");
    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);
    assertEquals(new BigDecimal("23456"), s.nextBigDecimal());
    s.useLocale(Locale.GERMANY);
    assertEquals(new BigDecimal("23.456"), s.nextBigDecimal());
    s = new Scanner("23.456 23.456");
    s.useLocale(Locale.ENGLISH);
    assertEquals(new BigDecimal("23.456"), s.nextBigDecimal());
    s.useLocale(Locale.GERMANY);
    assertEquals(new BigDecimal("23456"), s.nextBigDecimal());
    s = new Scanner("23,456.7");
    s.useLocale(Locale.ENGLISH);
    assertEquals(new BigDecimal("23456.7"), s.nextBigDecimal());
    s = new Scanner("-123.4");
    s.useLocale(Locale.ENGLISH);
    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)

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