944,054 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1958
  • Java RSS
Jul 1st, 2005
0

tokenizer

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scb10 is offline Offline
4 posts
since Jun 2005
Jul 1st, 2005
0

Re: tokenizer

Will this ID always be in that format, or will the comma and such be in different places?
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 1st, 2005
0

Re: tokenizer

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?

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 1st, 2005
0

Re: tokenizer

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
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Jul 4th, 2005
0

Re: tokenizer

it could be anywhere so i need a code where no matters where the comma is
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scb10 is offline Offline
4 posts
since Jun 2005
Jul 4th, 2005
0

Re: tokenizer

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scb10 is offline Offline
4 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: object oriented programme help
Next Thread in Java Forum Timeline: Help with a java program home work assignment





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC