Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

check whether returned vector element is String or integer

 
Amar Samant
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amar Samant
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic