Java Integer Create toInteger(Object str)

Here you can find the source of toInteger(Object str)

Description

to Integer

License

Open Source License

Declaration

public static int toInteger(Object str) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static int toInteger(Object str) {
        return str == null ? 0 : str instanceof Number ? ((Number) str).intValue() : toInteger(str.toString());
    }//from   w  w w . j ava 2 s  .  c o  m

    public static int toInteger(String str) {
        if (str == null) {
            return 0;
        } else {
            str = str.trim();
            if (str.length() == 0) {
                return 0;
            } else {
                int i = isNumeric(str);
                return i == 1 ? Integer.parseInt(str) : (i == 2 ? Double.valueOf(str).intValue() : 0);
            }
        }
    }

    public static int isNumeric(String str) {
        if (str == null) {
            return 0;
        } else {
            boolean isdouble = false;
            boolean hasE = false;
            int i = str.length();

            while (true) {
                while (true) {
                    char c;
                    do {
                        --i;
                        if (i < 0) {
                            if (isdouble) {
                                return 2;
                            }

                            return 1;
                        }

                        c = str.charAt(i);
                    } while (i == 0 && c == 45);

                    if (c == 46) {
                        if (isdouble) {
                            return 0;
                        }

                        isdouble = true;
                    } else if (c != 69 && c != 101) {
                        if (!Character.isDigit(str.charAt(i))) {
                            return 0;
                        }
                    } else {
                        if (hasE) {
                            return 0;
                        }

                        hasE = true;
                    }
                }
            }
        }
    }
}

Related

  1. toInteger(Object obj)
  2. toInteger(Object object)
  3. toInteger(Object object)
  4. toInteger(Object object, int defaultValue)
  5. toInteger(Object object, Integer defaultValue)
  6. toInteger(Object val)
  7. toInteger(Object val)
  8. toInteger(Object value)
  9. toInteger(Object value)