954,180 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

tokenizer

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

scb10
Newbie Poster
4 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Will this ID always be in that format, or will the comma and such be in different places?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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?

import java.util.*;

class TestTokenizer
{
	public static void main(String[] args)
	{
		ArrayList alTokens = new ArrayList();

		String id = "SD125,SD478-SD478 SD147";
		System.out.println("Original ---> " + id);
		
		int commaIndex = id.indexOf(",");
		int dashIndex = id.indexOf("-");
		int spaceIndex = id.indexOf(" ");
		
		StringTokenizer st = new StringTokenizer(id,",- ");

		while (st.hasMoreTokens())
		{
			alTokens.add(st.nextToken());
		}
		
		StringBuffer sb = new StringBuffer();
		for (int i=0; i<alTokens.size(); i++)
		{
			System.out.println(alTokens.get(i).toString());
			sb.append(alTokens.get(i).toString());
		}
		sb.insert(commaIndex,",");
		sb.insert(dashIndex,"-");
		sb.insert(spaceIndex," ");
		System.out.println("Back to original order---> " + sb.toString());
	}
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

it could be anywhere so i need a code where no matters where the comma is

scb10
Newbie Poster
4 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

scb10
Newbie Poster
4 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You