Results 1 to 11 of 11
  1. #1
    ScaryJello is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default Assigning Words to an Array

    Ok so i have this assignment. I have a shell file that reads and writes and tokenizes the file and takes the words and adds them to the output file. The shell works fine but for the assignment, i need to do little bit more.

    I need to pass the words into an array of strings obvi. But heres the catch, if a word appears more than once in the text file, the array is only suppose to contain the word once. Basically, what the array is to contain is a lit of alll the words in the text file.

    So i need to know how to fill the array from the text file and and if anyone has a method on how to alphabetize the words, that would help too.

    I use Textpad to write code
    Attached Files Attached Files

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

    Default

    The way I'd do it would be to put the Strings into a HashSet<String>. ;)

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    you mean using something like this ?
    String [] a=array.split();
    strings to array )
    that helped? report

  4. #4
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    I recently wrote a program that Scrambles words in a file, I'll attach it. Look over it, check how I did it. Note: I only used StringTokenizer in it because I needed to keep the delimiters. You should use regular expressions unless you need to count the delimiters as tokens. Save the file as .java so you can see syntax hilighting in an IDE (or TextPad). The only thing my class doesn't have is checking to not have the same word twice, but that wouldn't be hard - using an ArrayList and its contains method jumps to mind. Hope this helps.
    -MK12
    Attached Files Attached Files
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Use of regular expressions is much better. StringTokernizer can be deprecated soon with the latest JDK release.

  6. #6
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    I know, but with regular expressions it is impossible to count the delimiters as tokens. You can with StringTokenizer. That's the only reason to use it. In future release, if they deprecate StringTokenizer, they must also allow regular expressions to count delimiters as tokens.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  7. #7
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    @MK12 - it's not impossible (look at look-behind and look-ahead matching), but it is a bit clumsy.

  8. #8
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    It's not worth the extra effort to try doing it with regex instead of StringTokenizer when you need the delimiters counted as tokens.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by MK12 View Post
    I know, but with regular expressions it is impossible to count the delimiters as tokens. You can with StringTokenizer. That's the only reason to use it. In future release, if they deprecate StringTokenizer, they must also allow regular expressions to count delimiters as tokens.
    -MK12
    I agreed with you. That's why I say use of regular expressions is much better, not the only way. Reason is as neilcoffey says, counting tokens in regular expressions is really mess.

  10. #10
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Yeah I know.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  11. #11
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by ScaryJello
    how to fill the array from the text file and
    what you mean saying array from text file... so you have a code array in your text file or you just mean to take string and then seperate each word in it and make an array of it? or you have a list in your text file and just want to read it into your array? quite confused (((

Similar Threads

  1. declaring fields without assigning values to them
    By diggitydoggz in forum New To Java
    Replies: 12
    Last Post: 01-03-2009, 08:22 PM
  2. Need help with 2D array program. Crossing words
    By busdude in forum New To Java
    Replies: 3
    Last Post: 11-17-2008, 02:16 AM
  3. Assigning a string value to a char
    By coffeebean in forum New To Java
    Replies: 4
    Last Post: 06-15-2008, 06:30 AM
  4. Assigning Threads on Multiprocessor?
    By DrRufus in forum Advanced Java
    Replies: 1
    Last Post: 08-08-2007, 10:55 PM
  5. Replies: 1
    Last Post: 08-06-2007, 11: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
  •