Parsing objects from a string in Java

I am trying to write a general method for analyzing objects from strings. To be clear, I have the following not-so-elegant implementation:

public static Object parseObjectFromString(String s, Class class) throws Exception { String className = class.getSimpleName(); if(className.equals("Integer")) { return Integer.parseInt(s); } else if(className.equals("Float")) { return Float.parseFloat(s); } else if ... } 

Is there a better way to implement this?

+6
java reflection types
source share
6 answers

Your method can have one line of code:

 public static <T> T parseObjectFromString(String s, Class<T> clazz) throws Exception { return clazz.getConstructor(new Class[] {String.class }).newInstance(s); } 

Testing using different classes:

 Object obj1 = parseObjectFromString("123", Integer.class); System.out.println("Obj: " + obj1.toString() + "; type: " + obj1.getClass().getSimpleName()); BigDecimal obj2 = parseObjectFromString("123", BigDecimal.class); System.out.println("Obj: " + obj2.toString() + "; type: " + obj2.getClass().getSimpleName()); Object obj3 = parseObjectFromString("str", String.class); System.out.println("Obj: " + obj3.toString() + "; type: " + obj3.getClass().getSimpleName()); Object obj4 = parseObjectFromString("yyyy", SimpleDateFormat.class); System.out.println("Obj: " + obj4.toString() + "; type: " + obj4.getClass().getSimpleName()); 

Exit:

 Obj: 123; type: Integer Obj: str; type: String Obj: 123; type: BigDecimal Obj: java.text.SimpleDateFormat@38d640 ; type: SimpleDateFormat 
+12
source share

I'm not sure what you are trying to do. Here are a few different guesses:

  • You want to be able to convert an object to a string and vice versa.

You should study serialization. I use XStream , but writeObject and java.beans.XMLEncoder also works.

  • The user enters the text, and you want to force it to the "correct" type, which is a lot.

This usually means a problem with the user specification. What do you get from the user, and why can he have so many different kinds?

In general, you need the type to be as wide as possible: use double if it is a number, and String for almost everything else. Then create other things from this variable. But don't go on to type: usually a type should be very obvious.

+8
source share

What about transfers?

  public enum Types { INTEGER { @Override public Object parse(String s) { return Integer.parseInt(s); } }, FLOAT { @Override public Object parse(String s) { return Float.parseFloat(s); } } ... ; public abstract Object parse(String s); public static Object parseObjectFromString(String s, Class<?> cls) { return valueOf(cls.getSimpleName().toUpperCase()).parse(s); } } public static void main(String[] args) { System.out.println(Types.parseObjectFromString("5", Integer.class)); } 
+2
source share

NumberUtils.createNumber(str) (from apache commons-lang)

It decides which type of number to create, so you do not go through the class.

+1
source share

If these are your only example types, you can also do something like:

 Constructor ctor = Class.getConstructor( String.class ); return ctor.newInstance( s ); 

Additional information may provide better answers. I would be very surprised if there is not yet a utility code that meets your requirements. There are several ways to solve this problem, depending on actual requirements.

0
source share

To convert from an object (here is a string) to another, you can use transmorph :

 Transmorph transmorph = new Transmorph(new DefaultConverters()); float myFloat = transmorph.convert("55.2",Float.TYPE); long[] longsArray = transmorph.convert(new Integer[] { 1, 2, 3, 4 }, long[].class); 
0
source share

All Articles