Search in sources :

Example 96 with NoSuchElementException

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

the class ScannerTest method test_skip_LPattern.

/**
     * @tests java.util.Scanner#skip(Pattern)
     */
public void test_skip_LPattern() {
    s = new Scanner("test");
    try {
        s.skip((String) null);
        fail();
    } catch (NullPointerException expected) {
    }
    // If pattern does not match, NoSuchElementException will be thrown out.
    s = new Scanner("1234");
    try {
        s.skip(Pattern.compile("\\p{Lower}"));
        fail();
    } catch (NoSuchElementException expected) {
    }
    // Then, no matchResult will be thrown out.
    try {
        s.match();
        fail();
    } catch (IllegalStateException expected) {
    }
    s.skip(Pattern.compile("\\p{Digit}"));
    MatchResult matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(1, matchResult.end());
    s.skip(Pattern.compile("\\p{Digit}+"));
    matchResult = s.match();
    assertEquals(1, matchResult.start());
    assertEquals(4, matchResult.end());
    s.close();
    try {
        s.skip(Pattern.compile("test"));
        fail();
    } catch (IllegalStateException expected) {
    }
    MockStringReader2Read reader = new MockStringReader2Read("test");
    s = new Scanner(reader);
    try {
        s.skip(Pattern.compile("\\p{Digit}{4}"));
        fail();
    } catch (NoSuchElementException expected) {
    }
    try {
        s.match();
        fail();
    } catch (IllegalStateException expected) {
    }
    s.skip(Pattern.compile("\\p{Digit}{3}\\p{Lower}"));
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(4, matchResult.end());
    s.close();
    try {
        s.skip((Pattern) null);
        fail();
    } catch (IllegalStateException expected) {
    }
    StringBuilder stringBuilder = new StringBuilder();
    char[] chars = new char[1024];
    Arrays.fill(chars, 'a');
    stringBuilder.append(chars);
    stringBuilder.append('3');
    s = new Scanner(stringBuilder.toString());
    s.skip(Pattern.compile("\\p{Lower}+\\p{Digit}"));
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(1025, matchResult.end());
    // Large amount of input may be cached
    chars = new char[102400];
    Arrays.fill(chars, 'a');
    stringBuilder = new StringBuilder();
    stringBuilder.append(chars);
    s = new Scanner(stringBuilder.toString());
    s.skip(Pattern.compile(".*"));
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(102400, matchResult.end());
    // skip something without risking a NoSuchElementException
    s.skip(Pattern.compile("[ \t]*"));
    matchResult = s.match();
    assertEquals(102400, matchResult.start());
    assertEquals(102400, matchResult.end());
}
Also used : Scanner(java.util.Scanner) MatchResult(java.util.regex.MatchResult) NoSuchElementException(java.util.NoSuchElementException)

Example 97 with NoSuchElementException

use of java.util.NoSuchElementException 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 98 with NoSuchElementException

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

the class ScannerTest method test_hasNextBoolean.

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

Example 99 with NoSuchElementException

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

Example 100 with NoSuchElementException

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

the class StringTokenizerTest method test_nextToken.

/**
     * java.util.StringTokenizer#nextToken()
     */
public void test_nextToken() {
    // Test for method java.lang.String
    // java.util.StringTokenizer.nextToken()
    StringTokenizer st = new StringTokenizer("This is a test String");
    assertEquals("nextToken returned incorrect value", "This", st.nextToken());
    assertEquals("nextToken returned incorrect value", "is", st.nextToken());
    assertEquals("nextToken returned incorrect value", "a", st.nextToken());
    assertEquals("nextToken returned incorrect value", "test", st.nextToken());
    assertEquals("nextToken returned incorrect value", "String", st.nextToken());
    try {
        st.nextToken();
        fail("nextToken failed to throw a NoSuchElementException when it should have been out of elements");
    } catch (NoSuchElementException e) {
        return;
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) 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