Java Integer Create toInteger(String string)

Here you can find the source of toInteger(String string)

Description

to Integer

License

MIT License

Declaration

public static int toInteger(String string) 

Method Source Code

//package com.java2s;
/*//from  ww  w  . j  a  v a 2  s.c  o m
 * oxCore is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

public class Main {
    public static int toInteger(String string) {
        if (isEmpty(string)) {
            return 0;
        }

        try {
            return Integer.parseInt(string);
        } catch (NumberFormatException ex) {
            return 0;
        }
    }

    public static int toInteger(String string, int defaultValue) {
        if (isEmpty(string)) {
            return defaultValue;
        }

        try {
            return Integer.parseInt(string);
        } catch (NumberFormatException ex) {
            return defaultValue;
        }
    }

    public static Integer toInteger(String string, Integer defaultValue) {
        if (isEmpty(string)) {
            return defaultValue;
        }

        try {
            return Integer.parseInt(string);
        } catch (NumberFormatException ex) {
            return defaultValue;
        }
    }

    public static boolean isEmpty(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }

        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(str.charAt(i)) == false) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. toInteger(String s)
  2. toInteger(String s, int defValue)
  3. toInteger(String str)
  4. toInteger(String str)
  5. toInteger(String str)
  6. toInteger(String string, Integer deff)
  7. toInteger(String val)
  8. toInteger(String value)
  9. toInteger(String value)