Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

different token

Status
Not open for further replies.

scb10

Programmer
Jul 4, 2005
1
MX
i need to separate a string and identify by which delimeter i separated becouse at the end im going to unate again how do i do that???

i have this code

ArrayList alTokens = new ArrayList();

String id="Actualiza por Datos SD125,SD908-SD478 SD147 \n *,*,*,*,*,*,* \n 11/04/2004 14723.0125";
System.out.println("Original ---> \n " + 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---> \n " + sb.toString());

it separetes and when i put it together again i get

Back to original order--->
Actualiza porDatosSD125SD9,08SD4-78SD147
*******
11/04/200414723.0125



THE TROUBLE COMES WHEN I WANT TO GET TOGETHER THE STRING BECOUSE I DONT KNOW BY WHAT DELIMITER I SEPARATED THE STRING AND I DIDNT GET THE COMMA IN THE RIGTH PLACE I DONT KNOW WHY
 
Please only post "tips" when they are tips - not questions.
Also, please don't post questions with caps-lock on - it sounds like you are shouting.
Also, please post code between the tags "[ignore]
Code:
 and
[/ignore]".

With regards to your question, perhaps you should look at the String.split() method.

--------------------------------------------------
Free Database Connection Pooling Software
 
I don't fully understand what the code is doing, but I think you can use the StringTokenizer class, passing as an argument a String with all delimiters and true, that will return tokens and delimiters.

Cheers,

Dian
 
I'm hoping split worked out for you, but just as an exercise for learning (for both myself and others) I wrote what I thought the reasons for your code's failure on first run through. It will hopefully also allow you to gain insight into debugging code easier.

______

This appears to only grab the first instance of the delimeter (, - " ")

You would need an int[] to hold all the spots that each separator is actually there.
Use a loop with the "int indexOf(String str, int fromIndex) " method.

Code:
int commaIndex = id.indexOf(",");
int dashIndex = id.indexOf("-");
int spaceIndex = id.indexOf(" ");

Which is why your lines are getting chopped up and the 'Indexes' are not being put back in.
Code:
Back to original order--->
Actualiza porDatosSD125SD9,08SD4-78SD147
*******
11/04/200414723.0125

Your code comes back on three lines because the \n is being parsed as a newline character, I would imagine that you will need to strip those \'s and put them back in later like you did for the commas, spaces and dashes.

I'm not exactly sure why the commas get stripped out, but I imagine it has something to do with the .toString() method call on the tokenized string since your token is a dash (-), the token gets stripped out as best I understand tokenization.

HtH someone

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top