Java String Parse tryParse(String input, Object defVal)

Here you can find the source of tryParse(String input, Object defVal)

Description

try Parse

License

Apache License

Declaration

public static Object tryParse(String input, Object defVal) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static Object tryParse(String input, Object defVal) {
        if (input.equals("null"))
            return defVal;

        if (defVal instanceof Boolean) {
            return Boolean.valueOf(input);
        } else {/*from  w  w w.j  a va 2  s . c om*/
            if (input.equals("true"))
                return true;
            if (input.equals("false"))
                return false;
        }

        if ("[]".equals(input) && (defVal != null) && defVal.getClass().isArray())
            return defVal;

        try {
            return Integer.parseInt(input);
        } catch (NumberFormatException e) {
            if ((defVal instanceof Number) && !(defVal instanceof Double || defVal instanceof Float))
                throw e; // integer number expected but couldn't be parsed
        }

        try {
            return Double.parseDouble(input);
        } catch (NumberFormatException e) {
            if (defVal instanceof Number)
                throw e; // number expected but couldn't be parsed
        }

        try {
            return parseArrayOfInts(input);
        } catch (NumberFormatException e) {
            if (defVal instanceof int[])
                throw e; // int array expected
        }

        try {
            return parseArrayOfDoubles(input);
        } catch (NumberFormatException e) {
            if (defVal instanceof double[])
                throw e; // double array expected
        }

        return input;
    }

    public static int[] parseArrayOfInts(String input) {
        if (!(input.startsWith("[") && input.endsWith("]")))
            throw new NumberFormatException("Array should be enclosed in square brackets");
        String[] parts = input.substring(1, input.length() - 1).split(",");
        int[] res = new int[parts.length];
        for (int i = 0; i < parts.length; i++)
            res[i] = Integer.parseInt(parts[i].trim());
        return res;
    }

    public static double[] parseArrayOfDoubles(String input) {
        if (!(input.startsWith("[") && input.endsWith("]")))
            throw new NumberFormatException("Array should be enclosed in square brackets");
        String[] parts = input.substring(1, input.length() - 1).split(",");
        double[] res = new double[parts.length];
        for (int i = 0; i < parts.length; i++)
            res[i] = Double.parseDouble(parts[i].trim());
        return res;
    }
}

Related

  1. tryParse(String pValue)
  2. tryParse(String s, double d)
  3. tryParse(String s, int i)
  4. tryParse(String s, Integer defaultValue)