Results 1 to 6 of 6
  1. #1
    coder09 is offline Member
    Join Date
    Jan 2009
    Posts
    20
    Rep Power
    0

    Default StringTokenizer Question

    Hi,

    If I have a set of words in a text file, such as shown below, how do I choose to return only the words between the "ab" strings?

    Thank you.

    Text File Example:

    ab
    Hello
    How
    Are
    You
    Doing
    /ab

    abc
    What
    Time
    Is
    It
    /abc

    ab
    How
    Is
    The
    Weather
    /ab


    Here is my code so far:

    Java Code:
    FileReader fileReader = new FileReader("test.txt");
    BufferedReader in = new BufferedReader(fileReader);
    
    String delimiter = " []:<>\n\t,;?-()! \"\'";
    String test;
    
    while ((test = in.readLine()) != null) {
        StringTokenizer st = new StringTokenizer(test, delimiter);
        while (st.hasMoreTokens()) {
            String word = st.nextToken();            
            if (word.equals("ab")) {
    //need to loop through tokens until it equals “/ab”...
    //...but I get nosuchelementexceptions because I'm trying to create a... 
    //...secondary st.nextToken() instance.  I don't know the right way to do it...
    //...also, it needs to loop and return both sets of words between "ab"
            }
        }
    }

  2. #2
    DrKush's Avatar
    DrKush is offline Member
    Join Date
    Feb 2009
    Location
    Charloot NC
    Posts
    14
    Rep Power
    0

    Default

    did you import the needed classes?
    java.io/util?
    I've got an idea--an idea so smart that my head would explode if I even began to know what I'm talking about.

  3. #3
    coder09 is offline Member
    Join Date
    Jan 2009
    Posts
    20
    Rep Power
    0

    Default

    Yeah, I did. Thanks for pointing that out though. I just put the relevant code to look at, not the imports and such.

    Quote Originally Posted by DrKush View Post
    did you import the needed classes?
    java.io/util?

  4. #4
    DrKush's Avatar
    DrKush is offline Member
    Join Date
    Feb 2009
    Location
    Charloot NC
    Posts
    14
    Rep Power
    0

    Default

    try 'ab' not "ab"
    I've got an idea--an idea so smart that my head would explode if I even began to know what I'm talking about.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by DrKush View Post
    try 'ab' not "ab"
    No, no, no. That's wrong in so many ways.

    You'll likely either need a big-time regex (where's Darryl when you need him?) or you'll need to step through the text one line at a time yourself doing a simple parsing of the text.

    Edit: I'm wrong. You need to use booleans here to change the state of your program depending on whether you've passed "ab" and not "/ab". Perhaps only two boolean variables, abStart and abEnd, but no fancy regex is needed. Why not give it a try?

    Edit: wrong again. You can do this with only one boolean variable. And most importantly, you only read one token each time through your loop. It's all about the logic, the boolean logic. :)
    Last edited by Fubarable; 02-25-2009 at 11:34 PM.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Just off the top of my head... something like:

    • save = false
    • start loop
    • read line
    • if line equal to "ab"
    • save = true
    • else if line equal to "//ab"
    • break
    • if save == true
    • save the line
    • end loop


    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Question: BufferReader/StringTokenizer
    By VinceGuad in forum New To Java
    Replies: 1
    Last Post: 01-26-2009, 05:50 PM
  2. Help with StringTokenizer!
    By ookie833 in forum New To Java
    Replies: 13
    Last Post: 12-14-2008, 04:09 PM
  3. StringTokenizer
    By carderne in forum New To Java
    Replies: 1
    Last Post: 01-26-2008, 08:19 PM
  4. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:48 AM
  5. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •