Java Char Create toChar(Object value)

Here you can find the source of toChar(Object value)

Description

to Char

License

Open Source License

Declaration

public static char toChar(Object value) 

Method Source Code

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

public class Main {
    public static char toChar(Object value, char defaultValue) {
        try {//w  w  w . j  av  a  2 s . c  o m
            return toChar(value);
        } catch (RuntimeException e) {
            return defaultValue;
        }
    }

    public static char toChar(Object value) {
        if (null == value) {
            throw new RuntimeException("Impossible to convert "
                    + value
                    + " from "
                    + (null == value ? "unknown" : value.getClass()
                            .getName()) + " to " + char.class.getName());
        }
        if (value instanceof Character) {
            return ((Character) value).charValue();
        }
        if (value instanceof Number) {
            int int_value = ((Number) value).intValue();
            return (char) int_value;
        }
        if (value instanceof String && 1 == ((String) value).length()) {
            return ((String) value).charAt(0);
        }
        throw new RuntimeException("Impossible to convert " + value
                + " from "
                + (null == value ? "unknown" : value.getClass().getName())
                + " to " + char.class.getName());
    }
}

Related

  1. toChar(int i)
  2. toChar(int var)
  3. toChar(int[] arr)
  4. toChar(int[] arr)
  5. toChar(Object obj)
  6. toChar(short o)
  7. toChar(String chars)
  8. toChar(String delimiter)
  9. toChar(String parameter)