0

is there a way to cast any String automatically to its primitive data type in Java? For example having a List with 10 Strings:

string1 = "1234"
string2 = "12.34"
string3 = "String"
string4 = "0.53"
...

I would like to hand them all in a method and get the value back converted in its correct data type (Float, Integer, String):

int1 = 1234
float1 = 12.34
string1 = "String"
float2 = 0.53
...
8
  • 6
    briefly? No ... you have to implement it. Commented Jul 22, 2014 at 13:26
  • 2
    No. Nothing like that is built-in. You could write something close to it, but it would be ugly, hack-y and fragile. Commented Jul 22, 2014 at 13:27
  • 1
    Is this for some kind of serialisation? If so then there are better ways of doing this in Java. Commented Jul 22, 2014 at 13:30
  • 1
    Once you've written your function, what is its return type going to be? Commented Jul 22, 2014 at 13:34
  • 1
    This would actually be a bad bad practice, as there might be more than one possible types while casting, You should only choose to parse back when you are sure what the resulting type should be a generic implementation just doesn't make sense to me. Commented Jul 22, 2014 at 13:47

2 Answers 2

1

Simply achieve by RegEx

String string = "/**Place your value*/";
if (string.matches("\\d+")) {
 int i = Integer.parseInt(string); 
} else if (string.matches("^([+-]?\\d*\\.?\\d*)$)")) {
 float f = Float.parseFloat(string); 
}

In the same manner you can parse double, long, ....

Sign up to request clarification or add additional context in comments.

6 Comments

and how do you expect to distinguish float from int from String containing only digits etc using this technique?
by using regEx we can find its length or size, what ever we want. Come on man... ;)
"Come on man" is hardly an answer to the question. Neither length nor size (what's the difference anyway?) tells you if it's a string, an integer or a floating point value.
Casting is only meant for he cases when you are sure about what the resulting type would be, a generic hack would only favor a particular case in case of ambiguity. There is a reason why Java doesn't have such a hack in built.
*the as it would clearly flout the "Strongly Typed" Paradigm of Java.
|
-1

There is no way to do that. you can test an object for its class by using instanceof

Object integerValue = 1234;
    Object doubleValue = 12.34;
    Object array = new String[] { "This", "is", "a", "Stringarray" };
    if (integerValue instanceof Integer) {
        System.out.println("it's an Integer! Classname: " + integerValue.getClass().getName()); // will be printed
    }
    if (doubleValue instanceof Double) {
        System.out.println("this one is a Double *___* Classname: " + doubleValue.getClass().getName()); // will be printed
    }
    if (array instanceof String) {
        System.out.println("Is it a String?");
    } else if (array instanceof String[]) {
        System.out.println("It's a Stringarray :O! Classname: " + array.getClass().getName()); // will be printed
    } else {
        System.out.println("Huh? Something went wrong here :D");
    }

3 Comments

OP asked how to convert String to other types - not how to distinguish Object types.
And i said "There is no way to do that"!!! As you might know, you can get a class by its name. Class.forName(className); You must give the complete package path instead of its name, but so you can get its class. You want to cast/convert String into another object? so you must make a new instance of the targetclass with the value of the string. this is how you do it
Sadly, Basti, no, that's not how you do it - because your solution has Objects as an input, and OP explicitly stated that String is the input - you can't distinguish what String has inside by using instanceof, neither can you do that by casting String to Object, since the underlying instance would still be String. I see you're new to SO - please, instead of using multiple exclamation marks, try understanding the problem highlighted to you before dismissing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.