當前位置: 首頁>>代碼示例>>Java>>正文


Java StreamTokenizer.ordinaryChars方法代碼示例

本文整理匯總了Java中java.io.StreamTokenizer.ordinaryChars方法的典型用法代碼示例。如果您正苦於以下問題:Java StreamTokenizer.ordinaryChars方法的具體用法?Java StreamTokenizer.ordinaryChars怎麽用?Java StreamTokenizer.ordinaryChars使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.io.StreamTokenizer的用法示例。


在下文中一共展示了StreamTokenizer.ordinaryChars方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: parseElements

import java.io.StreamTokenizer; //導入方法依賴的package包/類
/**
 * <p>Parse an incoming String of the form similar to an array initializer
 * in the Java language into a <code>List</code> individual Strings
 * for each element, according to the following rules.</p>
 * <ul>
 * <li>The string is expected to be a comma-separated list of values.</li>
 * <li>The string may optionally have matching '{' and '}' delimiters
 *   around the list.</li>
 * <li>Whitespace before and after each element is stripped.</li>
 * <li>Elements in the list may be delimited by single or double quotes.
 *  Within a quoted elements, the normal Java escape sequences are valid.</li>
 * </ul>
 *
 * @param svalue String value to be parsed
 * @return The parsed list of String values
 *
 * @throws ConversionException if the syntax of <code>svalue</code>
 *  is not syntactically valid
 * @throws NullPointerException if <code>svalue</code>
 *  is <code>null</code>
 */
protected List parseElements(String svalue) {

    // Validate the passed argument
    if (svalue == null) {
        throw new NullPointerException();
    }

    // Trim any matching '{' and '}' delimiters
    svalue = svalue.trim();
    if (svalue.startsWith("{") && svalue.endsWith("}")) {
        svalue = svalue.substring(1, svalue.length() - 1);
    }

    try {

        // Set up a StreamTokenizer on the characters in this String
        final StreamTokenizer st =
            new StreamTokenizer(new StringReader(svalue));
        st.whitespaceChars(',',','); // Commas are delimiters
        st.ordinaryChars('0', '9');  // Needed to turn off numeric flag
        st.ordinaryChars('.', '.');
        st.ordinaryChars('-', '-');
        st.wordChars('0', '9');      // Needed to make part of tokens
        st.wordChars('.', '.');
        st.wordChars('-', '-');

        // Split comma-delimited tokens into a List
        final ArrayList list = new ArrayList();
        while (true) {
            final int ttype = st.nextToken();
            if ((ttype == StreamTokenizer.TT_WORD) ||
                (ttype > 0)) {
                list.add(st.sval);
            } else if (ttype == StreamTokenizer.TT_EOF) {
                break;
            } else {
                throw new ConversionException
                    ("Encountered token of type " + ttype);
            }
        }

        // Return the completed list
        return (list);

    } catch (final IOException e) {

        throw new ConversionException(e);

    }



}
 
開發者ID:yippeesoft,項目名稱:NotifyTools,代碼行數:75,代碼來源:AbstractArrayConverter.java


注:本文中的java.io.StreamTokenizer.ordinaryChars方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。