aspose file tools*
The moose likes I/O and Streams and the fly likes Another method to break a string ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Another method to break a string ???" Watch "Another method to break a string ???" New topic
Author

Another method to break a string ???

Roy Sin
Greenhorn

Joined: Mar 12, 2002
Posts: 10
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

Joined: Mar 12, 2002
Posts: 10
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

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Roy Sin
Greenhorn

Joined: Mar 12, 2002
Posts: 10
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

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Mar 12, 2002
Posts: 10
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

Joined: Dec 03, 2001
Posts: 125
There are also StringTokenizer replacements that will return null tokens. I've written one:
http://ostermiller.org/utils/StringTokenizer.html
 
jQuery in Action, 2nd edition
 
subject: Another method to break a string ???