Turns a string into a Number and returns a default value if unsuccessful. - Java java.lang

Java examples for java.lang:String Parse

Description

Turns a string into a Number and returns a default value if unsuccessful.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String s = "java2s.com";
        Number def = new Integer(42);
        System.out.println(parseNumber(s, def));
    }/*w w  w.j ava 2s .co m*/

    /** Turns a string into a Number and returns a default value if unsuccessful.
     * @param s the string to convert
     * @param d the default value
     * @return the numeric value
     */
    public static Number parseNumber(String s, Number def) {
        if (s == null)
            return def;

        s = s.trim();
        if (s.length() == 0)
            return def;

        int firstDigit = -1;
        for (int i = 0; i < s.length(); i++) {
            if (Character.isDigit(s.charAt(i))) {
                firstDigit = i;
                break;
            }
        }

        if (firstDigit < 0)
            return def;

        int lastDigit = firstDigit + 1;
        while (lastDigit < s.length()
                && Character.isDigit(s.charAt(lastDigit)))
            lastDigit++;

        boolean isDouble = false;

        if (firstDigit > 0 && s.charAt(firstDigit - 1) == '.') {
            firstDigit--;
            isDouble = true;
        }

        if (firstDigit > 0 && s.charAt(firstDigit - 1) == '-')
            firstDigit--;

        if (lastDigit < s.length() && s.charAt(lastDigit) == '.') {
            lastDigit++;
            while (lastDigit < s.length()
                    && Character.isDigit(s.charAt(lastDigit)))
                lastDigit++;

            isDouble = true;
        }

        if (lastDigit < s.length() && s.charAt(lastDigit) == 'E') {
            lastDigit++;
            while (lastDigit < s.length()
                    && Character.isDigit(s.charAt(lastDigit)))
                lastDigit++;

            isDouble = true;
        }

        final String actual = s.substring(firstDigit, lastDigit);

        if (isDouble || actual.length() > 17)
            return Double.parseDouble(actual);

        if (actual.length() > 10)
            return Long.parseLong(actual);

        return Integer.parseInt(actual);
    }

    /** Turns a string into an int and returns a default value if unsuccessful.
     * @param s the string to convert
     * @param d the default value
     * @return the int value
     */
    public static int parseInt(String s, int def) {
        return parseInt(s, def, null, true);
    }

    /** Turns a string into an int and returns a default value if unsuccessful.
     * @param s the string to convert
     * @param d the default value
     * @param lastIdx sets lastIdx[0] to the index of the last digit
     * @param allowNegative if negative numbers are valid
     * @return the int value
     */
    public static int parseInt(String s, int def, final int[] lastIdx,
            final boolean allowNegative) {
        final boolean useLastIdx = lastIdx != null && lastIdx.length > 0;
        if (useLastIdx)
            lastIdx[0] = -1;

        if (s == null)
            return def;

        s = s.trim();
        if (s.length() == 0)
            return def;

        int firstDigit = -1;
        for (int i = 0; i < s.length(); i++) {
            if (Character.isDigit(s.charAt(i))) {
                firstDigit = i;
                break;
            }
        }

        if (firstDigit < 0)
            return def;

        int lastDigit = firstDigit + 1;
        while (lastDigit < s.length()
                && Character.isDigit(s.charAt(lastDigit)))
            lastDigit++;

        if (allowNegative && firstDigit > 0
                && s.charAt(firstDigit - 1) == '-')
            firstDigit--;

        if (useLastIdx)
            lastIdx[0] = lastDigit;
        return Integer.parseInt(s.substring(firstDigit, lastDigit));
    }
}

Related Tutorials