• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

seprating number from variables

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have array of strings and have a need to seprate the variables from numbers. Is there a cleaner way of doing it in java. I know I can attempt to do an operation on every string and if operation results in exception that it is a variable otherwise a number.Please suggest a better way.

e.g array[0]= "1" , array[1]= "xyz" , array[2]= "1.9" , array[3]= "abc"

I need array_of_variables[]= {"xyz","abc"};
array_of_numbers[]={"1","1.9"}

Thanks for you suggestions in advance.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you don't know regex, I'll break it down for you. ^[0-9.]+$ basically means that from start (^) to finish ($), this string only has the numbers 0 through 9 or a period in it ([0-9.]), and at least one (+) of these.

Edit: If you want to include negative numbers, then put a - inside the brackets. Or, if you want to be more precise use ^-{0,1}[0-9.]+$
[ June 23, 2004: Message edited by: Darin Niard ]
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If possible, don't store the information as String objects in the first place. A String is a very, very loosely typed way to keep information...think about it, what information can any object store that can't be stored in a String?

Instead, use Integer objects to keep the numbers (or Double, or whatever level of precision you require...or simply use primitives) and create a new class Variable to represent the variables. That way, you can do neat-o things like:



Now you can have a variable object that actually is settable to a value. If you decide you want your numbers to be full-fledged objects instead of primitives, then just change all the getter/setter above to deal directly with Double objects instead of double primitives.

sev
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Darin!

I read ur post here and u mentioned something about "regex". I understood what u explained here.
I was totally unaware of this thing in java and am very much excited to know more and more about any such things in java.

Can u please refer me to any Link where I can study and understand such things.
Thanks in advance,

Yogesh Kodgule
 
Ahmed Basheer
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,


I think doing it by regex works only in newer version of java, not in Jdk1.3.1. But thanks for suggestions. Going by the Sever suggestion I don't quite understand how I can choose which variables to put in variable object and which ones are absolute values. Sever could you please give me some examples here?

Thanks for you suggestions.
-A
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yogesh, I love regex. It's used a lot in perl, which was my first language, so I find myself using it often (probably too often) in Java. The API has a decent list of regex constructs.
 
Yogesh Kodgule
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Darin!
Thanks for opening a new door of java API for me.
You love regex and i love java....ultimately both feel satisfied.

thanks to Ahmed too ...as he posed the question and things came this way.
And the obvious thanks to the whole Java Community, who make my Java Coding experience as smoother as possible.

Best regards,
Yogesh
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahmed? Parwiz is that you?
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ahmed,

Welcome to the ranch, and I'm glad you got your question answered

I wanted to tell you that we require members to adhere to a naming policy. That is, a first name followed by a last name, and it should not be a name I recognize, so Don Juan is out. As a matter of fact, we love to encourage member to use their real names. Please modify your name accordingly.

Thanks,
M
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic