Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Default Remove Duplicate words from String

    Hy everybody,

    i m new in java, my problem is as follow,
    i have a text file called "test.txt" contains text with repeated words. I want to remove repeated words from "test.txt" and store the unique words into another file called "unique.txt". code help would be highly appreciated.



    Regards
    Alijavaprogrammer

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,346
    Rep Power
    6

    Default Re: Remove Duplicate words from String

    So where are you stuck? You're not going to get code, so I hope that "code help" actually means "help me to write the code".
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Remove Duplicate words from String

    Here are some suggestions:


    • Practice extracting words from a text file or string
    • Learn how to do file I/O.
    • Understand the Set interface and its implementations.


    All of these are covered to varying degrees in the tutorials mentioned in my signature.

    Regards,
    Jim
    Last edited by jim829; 12-20-2013 at 05:24 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Remove Duplicate words from String

    A few methods are possible here. The simplest method I can think off off the top of my head is to read each string individually and add it to a HashMap<String> (hashmaps do not allow duplicates) then from there, read from the HashMap and write to the new file.

    Edit: By each string I mean each line of text in the file (assuming each word is on a separate line) if the words are not on a separate line, you can use the String.split(String regex) method to split the line into individual words.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Remove Duplicate words from String

    @zFollette
    You should read the API before you post.
    HashMap's signature is: HashMap<K,V> with a key / value pair
    not: HashMap<String>

    jim829 has already suggested using a Set.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Remove Duplicate words from String

    Quote Originally Posted by Norm View Post
    @zFollette
    You should read the API before you post.
    HashMap's signature is: HashMap<K,V> with a key / value pair
    not: HashMap<String>

    jim829 has already suggested using a Set.
    I was really dreading you would say that, I was going to fix my code and do something like HashMap<String, null>, but a Christmas Party got in the way of that.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Remove Duplicate words from String

    Actually, the HashSet implementation of the Set interface employs an internal HashMap to provide the set characteristics. It takes advantage of the non-duplicate key requirement of a HashMap. If you don't already do so I would recommend you look at the Java source code for the various classes in the JDK. It is quite educational. It is easy to link the source to the class libraries in Eclipse and probably NetBeans. Not certain about other IDE's though.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. How to find duplicate words in string array?
    By chkontog in forum New To Java
    Replies: 2
    Last Post: 11-12-2012, 02:16 AM
  2. find index of duplicate words inside string
    By onions in forum New To Java
    Replies: 9
    Last Post: 05-09-2012, 04:48 PM
  3. how to remove duplicate lines from a file
    By trkece in forum New To Java
    Replies: 11
    Last Post: 02-16-2011, 07:01 AM
  4. remove duplicate word in the string
    By katturv in forum New To Java
    Replies: 7
    Last Post: 12-19-2010, 08:16 PM
  5. How to Remove Duplicate object from collection
    By santhoskumara in forum Advanced Java
    Replies: 4
    Last Post: 08-04-2010, 08:07 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
  •