tokenizer

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2005
Posts: 4
Reputation: scb10 is an unknown quantity at this point 
Solved Threads: 0
scb10 scb10 is offline Offline
Newbie Poster

tokenizer

 
0
  #1
Jul 1st, 2005
i have this string SD125,SD478-SD478 SD147
i need to separate,but i need to know which delimeter i use becouse i did this

StringTokenizer sd = new StringTokenizer(fileStringSD, ", -");

while (sd.hasMoreTokens())
{
String cad = sd.nextToken();
system.out.printl (cad);
}

it separetes and i get
SD125
SD478
SDSD147

THE TROUBLE COMES WHEN I WANT TO GET TOGETHER THE STRING BECOUSE I DONT KNOW BY WHAT DELIMITER I SEPARATED THE STRING
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: tokenizer

 
0
  #2
Jul 1st, 2005
Will this ID always be in that format, or will the comma and such be in different places?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: tokenizer

 
0
  #3
Jul 1st, 2005
You could do this if it has a comma,dash,space, and nothing else...Although I'm not sure why you want to go down such a rocky road..Can't you just store the original id in a string?

  1. import java.util.*;
  2.  
  3. class TestTokenizer
  4. {
  5. public static void main(String[] args)
  6. {
  7. ArrayList alTokens = new ArrayList();
  8.  
  9. String id = "SD125,SD478-SD478 SD147";
  10. System.out.println("Original ---> " + id);
  11.  
  12. int commaIndex = id.indexOf(",");
  13. int dashIndex = id.indexOf("-");
  14. int spaceIndex = id.indexOf(" ");
  15.  
  16. StringTokenizer st = new StringTokenizer(id,",- ");
  17.  
  18. while (st.hasMoreTokens())
  19. {
  20. alTokens.add(st.nextToken());
  21. }
  22.  
  23. StringBuffer sb = new StringBuffer();
  24. for (int i=0; i<alTokens.size(); i++)
  25. {
  26. System.out.println(alTokens.get(i).toString());
  27. sb.append(alTokens.get(i).toString());
  28. }
  29. sb.insert(commaIndex,",");
  30. sb.insert(dashIndex,"-");
  31. sb.insert(spaceIndex," ");
  32. System.out.println("Back to original order---> " + sb.toString());
  33. }
  34. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 8
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: tokenizer

 
0
  #4
Jul 1st, 2005
Hi everyone,

See the below thread for better understanding of the string tokenizer class

http://www.daniweb.com/techtalkforums/thread26132.html

Another thing is that if you want to split up a string try looking up the java String class api

Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 4
Reputation: scb10 is an unknown quantity at this point 
Solved Threads: 0
scb10 scb10 is offline Offline
Newbie Poster

Re: tokenizer

 
0
  #5
Jul 4th, 2005
it could be anywhere so i need a code where no matters where the comma is
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 4
Reputation: scb10 is an unknown quantity at this point 
Solved Threads: 0
scb10 scb10 is offline Offline
Newbie Poster

Re: tokenizer

 
0
  #6
Jul 4th, 2005
THANKS FOR THE HELP, I PROVED YOUR CODE THE TROUBLR HERE IS THAT NOT ALWAYS I HAVE THAT STRING I have a file like this

ExchangeNumbers
SD00171-SD00125,SD00258 SD00015
*,*,*,*,*,*
11/04/2004 14723.0125

What i need is to separate everything becouse i need to exchange the SD### STRING for another that i have in anoteher file thats why i need to know exactly by which symbol i separate the sting to at the end of the exchange put it again like this:

ExchangeNumbers
SF124-SF758,SF359 SF142
*,*,*,*,*,*
11/04/2004 14723.0125
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1754 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC