Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static Object getObject(String type, String value) throws Exception {

        type = type.toLowerCase();
        if ("boolean".equals(type))
            return Boolean.valueOf(value);
        if ("byte".equals(type))
            return Byte.valueOf(value);
        if ("short".equals(type))
            return Short.valueOf(value);
        if ("char".equals(type))
            if (value.length() != 1)
                throw new NumberFormatException("Argument is not a character!");
            else
                return Character.valueOf(value.toCharArray()[0]);
        if ("int".equals(type))
            return Integer.valueOf(value);
        if ("long".equals(type))
            return Long.valueOf(value);
        if ("float".equals(type))
            return Float.valueOf(value);
        if ("double".equals(type))
            return Double.valueOf(value);
        if ("string".equals(type))
            return value;
        else {
            Object objs[] = new String[] { value };
            return Class.forName(type).getConstructor(new Class[] { java.lang.String.class }).newInstance(objs);
        }
    }
}