Java Integer Create toInt(Object value, int defaultValue)

Here you can find the source of toInt(Object value, int defaultValue)

Description

to Int

License

Open Source License

Declaration

public static int toInt(Object value, int defaultValue) 

Method Source Code

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

public class Main {
    public static int toInt(Object value, int defaultValue) {
        try {/*  ww  w .  j ava  2  s. co m*/
            return toInt(value);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static int toInt(Object value) throws RuntimeException {
        if (null == value) {
            throw new RuntimeException("Impossible to convert "
                    + value
                    + " from "
                    + (null == value ? "unknown" : value.getClass()
                            .getName()) + " to " + int.class.getName());
        } else if (value instanceof Number) {
            return ((Number) value).intValue();
        } else if (value instanceof String) {
            try {
                return Integer.parseInt((String) value);
            } catch (NumberFormatException e) {
                throw new RuntimeException("Impossible to convert "
                        + value
                        + " from "
                        + (null == value ? "unknown" : value.getClass()
                                .getName()) + " to " + int.class.getName());
            }
        }
        throw new RuntimeException("Impossible to convert " + value
                + " from "
                + (null == value ? "unknown" : value.getClass().getName())
                + " to " + int.class.getName());
    }
}

Related

  1. toInt(Object val)
  2. toInt(Object val)
  3. toInt(Object value)
  4. toInt(Object value)
  5. toInt(Object value)
  6. toInt(Object value, int nullValue)
  7. toInt(Object vo)
  8. toInt(short leftShort, short rightShort)
  9. ToInt(short n1, short n2)