• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another method to break a string ???

 
Roy Sin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
does anyone know how to do this.. i need to break up a string with "|" as the delimiter..
the data is as follows :
1|2|3|||6||||
i am unable to extract the numberr 6 out.. pls help.. my code is as follows :
BufferedReader br = new BufferedReader(inputFile);
line = br.readLine();
StringTokenizer in = new StringTokenizer(line,"|",true);
while (in.hasMoreTokens())
{
lineRead = in.nextToken();
if (c==6)
{
System.out.println("U got the number 6");
}
c++;
}
 
Roy Sin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorri.. for the StringTokenizer,
it should be
StringTokenizer in = new StringTokenizer (line,"|")...
it is bcos i want to get the column data.. it is supposed to allow users to add or delete data between the delimiter...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If c is an int that starts with the value of 0 before the while-loop, and you have less than 6 tokens in "in", then c will never be 6. Moreover, if you have only 6 tokens in "in", then c will equal 6 after the last iteration of the loop, and still nothing would have been printed.
Note: your current StringTokenizer "in" has 4 tokens - consecutive delimiters (without text between them) don't produce a (null) token.
 
Roy Sin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okie... does anyone out there have any ideas of how to get pass this limitation... other than putting space between the delimiters ? anyway, fyi.. the int c starts with 1 .....
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roy Sin:
okie... does anyone out there have any ideas of how to get pass this limitation... other than putting space between the delimiters ? anyway, fyi.. the int c starts with 1 .....

If c starts as 1, it never gets to 6 in your current code.
Within the past 3 weeks (maybe 2 even) this topic was discussed in the Beginner's forum and one or two solutions were presented. Look at the top right of this page for the search page.
 
Roy Sin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hahahah.. it seems that i am the only one replying myself.. but hey.. i found out the solution.. so i m here to share it....
as u know.. there is a limitation for string tokenizer.. so to get ard the prob.. i counted the total no of elements in the row.. then i count the no. of elements that are not delimiters.. then i subtracted the two...
for e.g.
i want to get the number 6 from column 6....
|||4||6|

Hope it helps anyone with the same problem..
 
Steve Deadsea
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are also StringTokenizer replacements that will return null tokens. I've written one:
http://ostermiller.org/utils/StringTokenizer.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic