Results 1 to 6 of 6
  1. #1
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default Can't figure out string tokenizer

    So my problem is that I have to use string tokenizer for a project and we have to read from a file and make the data into tokens which we then have to put into 2 stacks implemented via doubly linked lists by the A(adds) and D(deletes). I have my doubly linked list class, linkstack class, and all other classes neccesary already set up. However, I cannot figure out string tokenizer and how to use it with a file.

    Let me know what you think.

    The file i have to use it this:
    Java Code:
    A     Trafalgar   TTTTT     Western_Europe     4     11111     
    D	   France
    D	U.A.E.
    A     Lapland	LLLLL     Northern_Europe    4     22222
    A     Saar     SSSSS     Western_Europe     3     33333
    D	Jordan
    D	India
    A     Yugoslavia  Belgrade  Central_Europe     4     44444
    D     Mongolia
    A	Yemen	Sanaa	    West_Asia       	4    66776
    D     Ireland
    A	Spain       Madrid    Western_Europe     3     33333
    D  Mongolia

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    First, you read your line in from the file (I'm thinking you know how to do this?).

    Second, use StringTokenizer's constructor to tokenize the string suing a delimiter (space in this case). StringTokenizer (Java Platform SE 6), java.lang.String)

    Then finally you can read the tokens[0] to find whether it's A or D, then go from there.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    However, I cannot figure out string tokenizer and how to use it with a file.

    As it stands this is a rather general observation - whereas you'll get the best help if you can frame a fairly specific question.

    Take it a bit at a time; string tokenisers and files. You will see from the API documentation that the StringTokenizer requires a string argument. So you have to obtain strings from your file.

    Your assignment will have described just how the tokens are arranged in the file. Very probably they come in lines - but check this. So a good place to start would be to write a small application that

    (1) Uses a loop to obtain the lines of the file one at a time (BufferedReader?)

    (2) In an inner loop (or a separate method) constructs a string tokeniser from the lines and uses the tokeniser's hasMoreTokens()/nextToken() methods to extract and print the tokens.

    At this point you will be able to check that the tokens you have printed are the same as those described by the assignment.

    Textbooks and online resources should be able to help with basic usage examples of the classes and methods involved. (I think I've mentioned them). exampledepot.com can often be useful:

    BufferedReader
    StringTokenizer

  4. #4
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    Hmmm, yea I'm using bufferedreader for the read in. A more specific part is making the tokens themselves because when I tried to pass the file to the tokenizer params (i.e. StringTokenizer st = new StringTokenizer(br1, " "); )
    I get the error:

    cannot find symbol
    symbol: constructor StringTokenizer(java.io.BufferedReader,java.lang.S tring)
    location: class java.util.StringTokenizer

    wondering why this is

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You get that error message because there is no way of building a string tokeniser out of a file. Both responses linked to the StringTokenizer documentation: it lists the constructors and there just isn't one that takes a file or a file reader as an argument.

    Hence the need to break this problem down into two steps as indicated by both responses. First obtain the lines of the file (assuming each record comes in the form of a line). Then construct a StringTokenizer instance from the line and use it to obtain the individual tokens. In code the "first" and "then" parts will be nested loops - since there will be lots of lines making up the file, and lots of tokens making up the line.

    If you decide to go this route some code showing what you are doing at the moment would be good. (and if it is not doing doing what you expect say what it does and what you *do* expect).

  6. #6
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    I think I get it. I'll try some more n see what I come up with. Wish I didn't have to use stringtokenizer but forced to in this project lol. Just wants us to learn it

Similar Threads

  1. String Tokenizer
    By sumaih in forum Java Gaming
    Replies: 2
    Last Post: 08-21-2010, 04:23 PM
  2. String Tokenizer
    By hussainian in forum Advanced Java
    Replies: 1
    Last Post: 03-16-2010, 09:58 AM
  3. String Tokenizer
    By viperlasson in forum New To Java
    Replies: 1
    Last Post: 03-09-2010, 02:14 PM
  4. String Tokenizer
    By redasu in forum Advanced Java
    Replies: 4
    Last Post: 02-19-2010, 04:30 AM
  5. string tokenizer
    By twinytwo in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 03:10 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
  •