Java String Tokenize getNameTokens(final String names)

Here you can find the source of getNameTokens(final String names)

Description

Return a List of name tokens

License

Creative Commons License

Parameter

Parameter Description
names is a comma separated String

Return

will a List of Strings

Declaration

public static List<String> getNameTokens(final String names) 

Method Source Code

//package com.java2s;
/**//from  www  .j  a va  2 s .c  o m
 * Generic utility class.
 * <p/>
 * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 United States License. To
 * view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/us/ or send a letter to Creative
 * Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
 *
 * @author $Author: jal55 $
 * @version $Rev: 8005 $
 * @package edu.psu.iam.cpr.core.util
 * @lastrevision $Date: 2013-09-11 08:47:10 -0400 (Wed, 11 Sep 2013) $
 */

import java.util.*;

public class Main {
    /**
     * Return a List of name tokens
     *
     * @param names is a comma separated String
     * @return will a List of Strings
     */
    public static List<String> getNameTokens(final String names) {
        List<String> listNames = new ArrayList<String>();
        if (fieldIsPresent(names)) {
            StringTokenizer stkn = new StringTokenizer(names, " ,");
            while (stkn.hasMoreTokens()) {
                String aName = stkn.nextToken();
                if (aName.trim().length() > 0) {
                    listNames.add(aName);
                }
            }
        }

        return listNames;
    }

    /**
     * check all fields supplied to see if they have data
     *
     * @param names a variable argument list of fields
     * @return 'true' if all fields contain data, else return 'false'
     */
    public static boolean fieldIsPresent(String... names) {
        /* false means at least one field is null or empty
         * true  means all fields have real data 
         */
        if (names == null) {
            return false;
        }

        boolean isPresent = true;

        // All fields must be present
        for (String name : names) {
            if (name == null || name.trim().length() == 0) {
                isPresent = false;
            }
        }

        return isPresent;
    }
}

Related

  1. extractTokens(String strStartToken, String strEndToken, String strExpression)
  2. extractTokens(String text, String delim)
  3. getAllTokens(final String string)
  4. getCommandTokens(String commandString)
  5. getFileName(StringTokenizer s)
  6. getParamFloat(StringTokenizer s)
  7. getParamString(StringTokenizer s)
  8. getSQLTokens(String query)
  9. getStringTokens(String str)