Java Integer Parse tryParseInts(String[] in)

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

Description

Tries to parse an array of integers.

License

Creative Commons License

Parameter

Parameter Description
in The input array. Must not be null

Return

The result

Declaration

public static int[] tryParseInts(String[] in) 

Method Source Code

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

public class Main {
    /**/*  w ww.j  av a2  s.c  om*/
     * Tries to parse an array of integers.
     * 
     * 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 int[] tryParseInts(String[] in) {
        try {
            return parseInts(in);
        } catch (NumberFormatException e) {
            return new int[0];
        }
    }

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

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

        return out;
    }
}

Related

  1. tryParseInt(String value)
  2. tryParseInt(String value)
  3. tryParseInteger(String intValue)
  4. tryParseInteger(String text)
  5. TryParseIntInvariant(String str, int[] n)