String to Object
Hello, I'm trying to find a method or something that let's me know the class of a String that has been created from a StringTokenizer.
For example, if I have a string s1 = "3.8"; I'd need something that tells me that this is a Double, or a string s2 = "true" to get a Boolean and so on. I'd need it for the following classes that might be in that string, Integer, Boolean, Double, String and Character.
I thought about turning those strings into Objects and the using the getClass() method but I was unable to do it.
I'd really appreciate your help.
Thanks!
[593 byte] By [
max_18a] at [2007-10-3 8:29:51]

> For example, if I have a string s1 = "3.8"; I'd need
> something that tells me that this is a Double,
It's not a Double. It's a String.
> or a
> string s2 = "true" to get a Boolean
It's not a Boolean. It's a String.
> I thought about turning those strings into Objects
> and the using the getClass() method but I was unable
> to do it.
Because they're Strings, not Doubles, etc.
You can walk through the parse methods:
try {
Double.parseDouble(str);
// it can represent a double if we get here
}
catch (ParseException exc) {
// it's not a double. log it, or just smother the exception and move on
}
try {
Long.parseLong(str)
}
... etc. ...
Of course, some strings can validly represent many types. For instance, "-128" through "127" can represent Byte, Short, Integer, Long, Float, Double. You'll have to decide what your rules are in those situations.
Hi,There is no such method or class. The only thing you can do is to take a look a the content of the String, and take a guess. You can then use the methods which can parse from a string. E.g Integer.parseInt(string)Kaj
*sigh* too slow, but I can blame it on the connection speed. I'm posting from an airplane which is on it's way to Europe from the US :)
Try searching the forums for isnumeric, ischar, or isdouble.
http://forum.java.sun.com/thread.jspa?forumID=31&threadID=610421
http://forum.java.sun.com/thread.jspa?forumID=31&threadID=545449
http://forum.java.sun.com/thread.jspa?forumID=31&threadID=202650
There are several diccussions on who to do the testing.
rykk
> *sigh* too slow, but I can blame it on the connection> speed. I'm posting from an airplane which is on it's> way to Europe from the US :)Where were you, and how was it? Business or pleasure?
All right.Thank you all for your help!
> > *sigh* too slow, but I can blame it on the
> connection
> > speed. I'm posting from an airplane which is on
> it's
> > way to Europe from the US :)
>
> Where were you, and how was it? Business or pleasure?
I was attending Software Summit in Keystone, so it was both business and pleasure :)
I liked the konference, it's rather small (about 400 people?) so you can talk to all of the speakers, and it has a very friendly atmosphere.
I will probably go there next year again.
Kaj
If you're using a StringTokenizer and you want to tell if something is a number rather than a bare string, you might want to look at using java.io.StreamTokenizer instead.Or maybe you can get a full-fleged lexer. There's probably one created for Java somewhere.
> I was attending Software Summit in Keystone, so it
> was both business and pleasure :)
>
> I liked the konference, it's rather small (about 400
> people?) so you can talk to all of the speakers, and
> it has a very friendly atmosphere.
Oh, yeah. I was there in '98 or '99. I really got a lot out of it. All technical, no BS vendor booths. Great ad-hoc after-session discussions. This was where I had the chance to talk to Bill Joy.
> Oh, yeah. I was there in '98 or '99. I really got a> lot out of it. All technical, no BS vendor booths.> Great ad-hoc after-session discussions. This was> where I had the chance to talk to Bill Joy.Nice. Do you think you will visit it again?
> > Oh, yeah. I was there in '98 or '99. I really got
> a
> > lot out of it. All technical, no BS vendor booths.
> > Great ad-hoc after-session discussions. This was
> > where I had the chance to talk to Bill Joy.
>
> Nice. Do you think you will visit it again?
I sure hope so. We'll see what my company's training budget and policies look like next year. :-)
> I sure hope so. We'll see what my company's training
> budget and policies look like next year. :-)
Now I know why it was free to use the wireless network on the plane, it wasn't reliable. Worked for a while, but the connection got terminated a few times.
I hope your company decides to send you to the conference :)
> *sigh* too slow, but I can blame it on the connection> speed. I'm posting from an airplane which is on it's> way to Europe from the US :)That plane's probably moving at Mach .75 or more. Your connection can't be that slow. ;)