Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;

import java.util.StringTokenizer;

public class Main {
    /**
     * gets a tokenizer and returns an arraylist with the separated elements of the tokenizer
     * 
     * @param strt the tokenizer
     * @param hasToTrim if has to trim every single element of the tokenizer
     * @return the AL with the elements
     */
    public static String[] getStringArrayFromString(String strValues, String strSeparator, boolean hasToTrim) {

        // if null
        if (strValues == null)
            return new String[0];

        // creates the AL
        ArrayList<String> al = new ArrayList<String>();
        StringTokenizer strt = new StringTokenizer(strValues, strSeparator);

        while (strt.hasMoreElements()) {
            // gets the next element
            String strElement = (String) strt.nextElement();

            // if is to be trimmed, it does it
            if (hasToTrim)
                strElement = strElement.trim();

            // and adds it to the AL
            al.add(strElement);
        }

        String[] strArray = new String[al.size()];

        // returns the AL
        return al.toArray(strArray);
    }
}