Java Float Number Parse tryParseFloats(String[] in)

Here you can find the source of tryParseFloats(String[] in)

Description

Tries to parse an array of floats.

License

Creative Commons License

Parameter

Parameter Description
in The input array. Must not be null

Return

The result

Declaration

public static float[] tryParseFloats(String[] in) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

public class Main {
    /**/*from   w w w  .j  ava  2 s .co  m*/
     * Tries to parse an array of floats.
     * 
     * Returns the parsed version of in or an empty array on any parse errors.
     * 
     * @param in The input array. Must not be null
     * @return The result
     */
    public static float[] tryParseFloats(String[] in) {
        try {
            return parseFloats(in);
        } catch (NumberFormatException e) {
            return new float[0];
        }
    }

    /**
     * Parses an array of floats.
     * 
     * @param in The input array. Must not be null
     * @return The result
     * @throws NumberFormatException If any element in in is not parsable
     */
    public static float[] parseFloats(String[] in) {
        float[] out = new float[in.length];

        for (int i = 0; i < in.length; i++) {
            out[i] = Float.parseFloat(in[i]);
        }

        return out;
    }
}

Related

  1. readFloatFromString(String inStr)
  2. string2float(String arg, Locale loc)
  3. tryParseFloat(String floatValue)
  4. tryParseFloat(String value)
  5. tryParseFloat(String value)