Java Array Create toArray(String str)

Here you can find the source of toArray(String str)

Description

Parses a formated string and returns an array.

License

Open Source License

Parameter

Parameter Description
String A formated string, i.e., (23,12,12,3)

Return

int[]

Declaration

public static int[] toArray(String str) 

Method Source Code

//package com.java2s;
/**//  w w  w .  j  a va2 s . c  o  m
 * Copyright (C) 2013-2016
 * Jeffrey Fulmer - <jeff@joedog.org>, et al.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *--
 */

public class Main {
    /**
     * Parses a formated string and returns an array. The string must
     * be formatted in the following manner: {n,n} or (n,n) or [n,n,n]
     * The array's size will be determined automatically based on the 
     * number of comma separated tokens inside the brackets.
     * <p>
     * @param  String  A formated string, i.e., (23,12,12,3)
     * @return int[]
     */
    public static int[] toArray(String str) {
        if (str == null && !str.matches("\\(.*|\\{.*|\\[.*")) {
            System.err.println("ERROR: Format should match: {n,n} (n,n) or [n,n]");
            return null;
        }

        String[] tmp = str.split(",");
        int size = tmp.length;

        if ((str = str.replaceAll("\\s", "")).length() < 2 * size + 1) {
            System.err.println("ERROR: Parsed length is too short: " + str.length());
            return null;
        }

        String[] tokens = (str = str.substring(1, str.length() - 1)).split(",");

        int[] ret = new int[tokens.length];
        try {
            for (int i = 0; i < size; ++i) {
                ret[i] = Integer.parseInt(tokens[i]);
            }
        } catch (NumberFormatException ex) {
            return null;
        }
        return ret;
    }
}

Related

  1. newArray(Object obj, int size)
  2. newArray(Object src, int len)
  3. newArray(String... args)
  4. toArray(String source, String delimiter)
  5. toArray(String str)
  6. toArray(String str, String split)
  7. toArray(String string)
  8. toArray(String text)
  9. toArray(T... items)