Results 1 to 8 of 8
  1. #1
    katturv is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default remove duplicate word in the string

    import java.util.HashSet;
    import java.util.Set;

    public class StringTest {
    public static void main(String[] args) {
    String s= "I came I saw I left";
    Set<String> s = new HashSet<String>();
    for (String a : args)
    if (!s.add(a))
    System.out.println("Duplicate detected: " + a);

    System.out.println(s.size() + " distinct words: " + s);
    }
    }


    i am getting complier error..please help
    i am trying to remove duplicate word in the string
    input:"I came I saw I left"
    i need output as :::"came saw left"

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

    Default

    You need to post the error message and state what line is causing it. I also see that you have two variables with the same name, "s". That won't work.

    edit: and do you really mean to loop through the command line arguments and ignore the String in the program? If so, I'd delete that String.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by katturv View Post
    import java.util.HashSet;
    import java.util.Set;

    public class StringTest {
    public static void main(String[] args) {
    String s= "I came I saw I left";
    Set<String> s = new HashSet<String>();
    for (String a : args)
    if (!s.add(a))
    System.out.println("Duplicate detected: " + a);

    System.out.println(s.size() + " distinct words: " + s);
    }
    }


    i am getting complier error..please help
    i am trying to remove duplicate word in the string
    input:"I came I saw I left"
    i need output as :::"came saw left"
    Wouldn't it me nice if you'd let us known what the exact compilation error was. Do we have to guess? What can we win? Is there any money involved here?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    katturv is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Hi
    basically i was trying to eliminate duplicate values frm string...
    i dnt knw where to declare the string variable
    i got this code from java website
    the error will be an obvious one...
    that it has been defined already....

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

    Default

    Quote Originally Posted by katturv View Post
    Hi
    basically i was trying to eliminate duplicate values frm string...
    i dnt knw where to declare the string variable
    i got this code from java website
    the error will be an obvious one...
    that it has been defined already....
    Please do not use non-standard abbreviations in your posts. It may seem like nit-picking, but it makes your posts hard to read, and since computer programming is all about precision, and one little spelling or capitalization error can spell doom, it is in your best interest to communicate your questions clearly. Also, many of the volunteers here don't speak English as a primary language and will have great difficulty understanding your post. You can edit the post above by clicking on the edit button to the lower right of the post.

    Thanks for your cooperation.

    Regarding your problem, don't borrow code that you don't understand else you'll end up with "Franken code". Instead I suggest that you start over and just write the method yourself from scratch.

    Luck.

  6. #6
    katturv is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    import java.util.HashSet;
    import java.util.Set;

    public class StringTest {
    public static void main(String[] args) {
    String s= "I came I saw I left";
    Set<String> s = new HashSet<String>();
    for (String a : args)
    if (!s.add(a))
    System.out.println("Duplicate detected: " + a);

    System.out.println(s.size() + " distinct words: " + s);
    }
    }

    where should i declare the syntax marked in orange it so that i get the following output....
    input:"I came I saw I left"
    i need output as :::"came saw left"

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

    Default

    Quote Originally Posted by katturv View Post
    import java.util.HashSet;
    import java.util.Set;

    public class StringTest {
    public static void main(String[] args) {
    String s= "I came I saw I left";
    Set<String> s = new HashSet<String>();
    for (String a : args)
    if (!s.add(a))
    System.out.println("Duplicate detected: " + a);

    System.out.println(s.size() + " distinct words: " + s);
    }
    }

    where should i declare the syntax marked in orange it so that i get the following output....
    input:"I came I saw I left"
    i need output as :::"came saw left"
    Again, you should dump this code and start over.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by katturv View Post
    import java.util.HashSet;
    import java.util.Set;

    public class StringTest {
    public static void main(String[] args) {
    String s= "I came I saw I left";
    Set<String> s = new HashSet<String>();
    for (String a : args)
    if (!s.add(a))
    System.out.println("Duplicate detected: " + a);

    System.out.println(s.size() + " distinct words: " + s);
    }
    }

    where should i declare the syntax marked in orange it so that i get the following output....
    input:"I came I saw I left"
    i need output as :::"came saw left"
    Even if your code compiled you would never get that output because the word "I" would be entered in that set at least once so your output would be "I came saw left". Think about your problem before you copy and paste random chunks of source code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. longest word present in a string
    By somnath6088 in forum New To Java
    Replies: 4
    Last Post: 11-09-2010, 05:43 PM
  2. how to remove word words from txt file?
    By amroosobhy in forum New To Java
    Replies: 3
    Last Post: 10-08-2010, 01:49 PM
  3. How to Remove Duplicate object from collection
    By santhoskumara in forum Advanced Java
    Replies: 4
    Last Post: 08-04-2010, 07:07 PM
  4. Remove duplicate lines from a text file
    By Dirt.Diver in forum New To Java
    Replies: 15
    Last Post: 06-25-2008, 02:08 PM
  5. random string are duplicate
    By googgoo in forum New To Java
    Replies: 3
    Last Post: 04-03-2008, 10:01 AM

Posting Permissions

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