The Artima Developer Community
Sponsored Link

Java Answers Forum
Getting around delimiters

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Nilkanth

Posts: 4
Nickname: nilkanth
Registered: Mar, 2003

Getting around delimiters Posted: Mar 1, 2003 7:27 PM
Reply to this message Reply
Advertisement
Hi! I m reading from a txt file. I tried many different ways but it's not working. I've problem w/ reading the following line:
prompt this "hello how r u", '"fine"', "'thanku,'" ; but ignore this

I want to get different tokens like:
prompt
this
hello
how
r
u
"fine"
'thanku'

Want to ignore the rest of the line starting with semicolon and want to store
'em in array. The code I m using is:
while ( (line = infile.readLine()) != null ) {
     StringTokenizer st = new StringTokenizer(line);
     while (st.hasMoreTokens()) {
    String s = st.nextToken();
    if (s.startsWith(";")) continue;
    if(s.endsWith(",")) s = s.replace(',', ' ');
    if(s.charAt(0) == '"') {
        while ((s.charAt(s.length() - 1) != '"')) {
            s += " " + st.nextToken();
        }
        s = s.substring(1, s.length() - 1);
    }
        System.out.println(s);
}

I get:
prompt
this
hello how r u", '"fine"', "'thanku,'
ignore

And if I return ","(comma) as a delimiter in the StringTokenizer, then I get:
prompt this "hello how r u"
'"fine"'
"'thanku
'" ; ignore

I tried it many ways but it doesn't work. Also I've no idea how to get these tokens and store them in array.
Pls help! I really appreciate ur help. Thanks in advance.

Topic: Java 1.4 slower in Win2k than Win98?! Previous Topic   Next Topic Topic: ARRAY PROBLEM

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use