It's not a secret anymore!*
The moose likes Java in General and the fly likes check whether returned vector element is String or integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "check whether returned vector element is String or integer" Watch "check whether returned vector element is String or integer" New topic
Author

check whether returned vector element is String or integer

Amar Samant
Greenhorn

Joined: Oct 06, 2007
Posts: 7
My Vector is returning elements such as [100,T009]

is it possible to check it whether the returned value is integer or string

i.e if it returns T009 then i want to get it recognized as String and if it returns 100 then i want to get it recognized as Integer.


SCJP 5.0
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Originally posted by Amar Samant:
My Vector is returning elements such as [100,T009]

is it possible to check it whether the returned value is integer or string

i.e if it returns T009 then i want to get it recognized as String and if it returns 100 then i want to get it recognized as Integer.


Can you post the code which you used for returning Vector...
Alonso Faust
Greenhorn

Joined: Dec 19, 2007
Posts: 14
You could use matcher(String).find() to check if a vector element loaded to a String contains non-digit characters and should therefore be pulled as a String. EG:

Java.Sun.Com has a complete listing of regex's to compile in Pattern.


"It is but the bedazzlement of the Great Enchanter which makes the Terrible Giants appear as windmills, to the untrained eye."
Amar Samant
Greenhorn

Joined: Oct 06, 2007
Posts: 7
Thanks ,i tried it by using Vector.get(i).toString().matches("[a-zA-Z].*\\d.*") by giving a pattern it is working for elements like [T009, 100] but it is failing for [T009,009T,100]
Alonso Faust
Greenhorn

Joined: Dec 19, 2007
Posts: 14
That's because the Pattern.matches() function returns true if AND ONLY IF the string matches the specified character sequence exactly. e.g.: the sequence "[a-zA-Z].\\d" will return true ONLY when the string starts with a sequence of letters and then contains some digits ("T009" returns True). If there is another non-digit character after the digits (or more) it will return false ("T009a" returns false). {note: "T009a" will return true with your sequence "[a-zA-Z].*\\d.*" because of the trailing *}

By compiling the pattern first, you can use the matcher.find() function which will return true if the sequence is found ANYWHERE in the string.
[ December 21, 2007: Message edited by: Alonso Faust ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: check whether returned vector element is String or integer