Java String Split by Separator splitDoubles(String str, String fieldSeparator)

Here you can find the source of splitDoubles(String str, String fieldSeparator)

Description

Convert a string of reals to a double array.

License

Open Source License

Parameter

Parameter Description
str String of numbers to convert
fieldSeparator String that separates each number

Return

an array of doubles

Declaration

public static double[] splitDoubles(String str, String fieldSeparator) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**/*  ww  w.  java  2s .com*/
     * Convert a string of reals to a double array.
     * This will not throw any exception if the string is not well formatted.
     * @param str String of numbers to convert
     * @param fieldSeparator String that separates each number
     * @return an array of doubles
     */
    public static double[] splitDoubles(String str, String fieldSeparator) {
        ArrayList array = new ArrayList();

        // split the string into tokens
        StringTokenizer tokenizer = new StringTokenizer(str, fieldSeparator);
        while (tokenizer.hasMoreTokens()) {
            array.add(tokenizer.nextToken());
        }

        // convert the tokens to double values and store them in values[]
        double[] values = new double[array.size()];
        for (int i = 0; i < values.length; i++) {
            try {
                values[i] = Double.parseDouble((String) (array.get(i)));
            } catch (Exception exc) {
                break;
            }
        }

        return values;
    }
}

Related

  1. split(String text, String separators, String brackets)
  2. splitAll(String str, char separatorChar)
  3. splitAndTrim(String tags, String separator)
  4. splitBySeparator(String path, String separator)
  5. splitByStringSeparator(String theString, String separatorString)
  6. splitField(String fieldWithSeparator, String separator)
  7. splitInts(String string, String separator)
  8. splitList(String source, char separator)
  9. splitListBySeparator(String text, String separator)