• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Another method to break a string ???

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic