Results 1 to 12 of 12
  1. #1
    trkece is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default how to remove duplicate lines from a file

    Hi all,

    I have the file with below content

    Ramakrishna
    Joseph
    Rabbani
    Ramakrishna
    Joseph
    Rabbani

    Pls help me to remove the duplicate lines and print like

    Ramakrishna
    joseph
    Rabbani

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by trkece View Post
    Hi all,

    I have the file with below content

    Ramakrishna
    Joseph
    Rabbani
    Ramakrishna
    Joseph
    Rabbani

    Pls help me to remove the duplicate lines and print like

    Ramakrishna
    joseph
    Rabbani
    A LinkedHashSet is made for this; read its API documentation for details.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    trkece is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    can u tell me clearly??

    i am new to java .........

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by trkece View Post
    can u tell me clearly??

    i am new to java .........
    Read the API documentation for that class, it can tell you much clearer how things work than I can; being new to Java is no excuse for not reading any documentation. I am not going to spoonfeed you the code either (it is just a couple of lines anyway).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Subhransu is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default Try it

    public static void main(String[] args) {
    try{
    FileInputStream fstream = new FileInputStream("D:/Files/Sample.txt");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    LinkedHashSet<String> set = new LinkedHashSet<String>();

    //Read File Line By Line
    while ((strLine = br.readLine()) != null) {
    // Print the content on the console
    set.add(strLine);
    // System.out.println (strLine);
    }
    System.out.println("Set :" + set);
    //Close the input stream
    in.close();
    }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Great, boiler plate code: an unformatted dump of code without any explanation. What is the OP supposed to learn from this? Spoonfeeding is a nono.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    reading the add javadoc for LinkedHashSet you will learn more than just hacking set.add(strLine) in your java editor.

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Subhransu, Please use code tags when posting code. Do you have a specific question? No one is going to write the program for you, but we will be happy to help with specific errors/questions. If you don't understand the definition of a set, hash based data structures, or linked lists, then now is the time to start reading some wiki's and ask your professor.

    Once you understand what these three things are, the solution should be obvious from a conceptual standpoint. Once thats the case, its a simple matter of doing it in java. The API tells you how to use the data structure.

    If you have trouble with specific lines of code or design decisions, then that is the time to come back here. People are far too lazy these days, show the community some good faith by demonstrating you have done your research first.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by quad64bit View Post
    Subhransu, Please use code tags when posting code. Do you have a specific question? No one is going to write the program for you, but we will be happy to help with specific errors/questions. If you don't understand the definition of a set, hash based data structures, or linked lists, then now is the time to start reading some wiki's and ask your professor.
    Sunhransu is not the OP (AFAICT) but supplied the spoonfeeding code. I already told him/her not to do it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Oh whoops! Man, I gotta stop posting when I first wake up :P Please, trkece, that post was for you. :D

  11. #11
    trkece is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    Thanx Subhransu ........i got the logic ..............

  12. #12
    Subhransu is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

Similar Threads

  1. how can we remove blank lines from a .txt
    By Camden in forum New To Java
    Replies: 12
    Last Post: 07-29-2011, 02:38 PM
  2. remove duplicate word in the string
    By katturv in forum New To Java
    Replies: 7
    Last Post: 12-19-2010, 08:16 PM
  3. How to Remove Duplicate object from collection
    By santhoskumara in forum Advanced Java
    Replies: 4
    Last Post: 08-04-2010, 08:07 PM
  4. How to remove 2 last lines in a text file?
    By Marius in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 04:54 PM
  5. Remove duplicate lines from a text file
    By Dirt.Diver in forum New To Java
    Replies: 15
    Last Post: 06-25-2008, 03:08 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
  •