Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

String Processing Question...

Thread view: 
Duke McPherson - 30 Mar 2006 03:15 GMT
// I have a string which contains data in the following format:

String myString = "0 Jay 1 Mike 2 Lindsay 3 Dude 4..."

// I want to put this data in a String[] array so that:
String myStringArray[] ;
myStringArray[0] = "Jay"
myStringArray[1] = "Mike"
myStringArray[2] = "Lindsay"
myStringArray[3] = "Dude"
//<etc.>

Is there a formal way of processing strings with a pre-determinded
token (space in this case) ?
Duke McPherson - 30 Mar 2006 03:53 GMT
Nevermind...   StringTokenizer is the answer...  Sorry for the bother...
Roedy Green - 30 Mar 2006 06:01 GMT
>Is there a formal way of processing strings with a pre-determinded
>token (space in this case) ?
four techniques come to mind:
see http://mindprod.com/jgloss/stringtokenizer.html
http://mindprod.com/jgloss/streamtokenizer.html
http://mindprod.com/jgloss/regex.html  split method
http://mindprod.com/jgloss/parser.html (if gets more complex)
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Ravi - 30 Mar 2006 12:07 GMT
If it is very clear that number and corresponding string comes in
alternative place,
then
1.Get the tokesn using Stringtokenizer
2.Iterate in the for loop
3.Using mod operation avoid the alternative position for number
4.Store the remaining string in requred array

I feel this case is optimization rather than reg expression or split
method.
Others correct me If I am wrong.
Oliver Wong - 30 Mar 2006 18:54 GMT
> If it is very clear that number and corresponding string comes in
> alternative place,
[quoted text clipped - 7 lines]
> method.
> Others correct me If I am wrong.

   Instead of using modulus to alternate between two processings (one for
number, one for name), you can just read two tokens in a row, e.g.

<pseudoCode>
StringTokenizer st = new StringTokenizer("foo");
while (st.hasMoreTokens()) {
 int number = Integer.parseInt(st.nextToken());
 String name = st.nextToken();
 //Process the numbers and name
}
</pseudoCode>

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.