9 Replies Latest reply: Apr 4, 2007 10:33 PM by 807599 RSS

    TreeSet question

    807599
      how can i use TreeSet in order to split the words and find the duplicates and the size? i created a program but it gives me error.
      import java.util.*;
      public class SpliExample {
           Scanner scanner = new Scanner(System.in);
           String input;
           Set<String> set = new TreeSet<String>();
           System.out.println("Enter the word ");
           input = scanner.next();
           set.add(input);
           for(String s : set)
                System.out.println(s);
      }
      identifier expected

      Message was edited by:
      lrngjava
        • 1. Re: TreeSet question
          807599
          You need to put all of that mess into a method, such as:
          import java.util.*;
          public class SpliExample {
          
               public static void main(String[] argv) {
                    Scanner scanner = new Scanner(System.in);
                    String input;
                    Set<String> set = new TreeSet<String>();
                    System.out.println("Enter the word ");
                    input = scanner.next();
                    set.add(input);
                    for(String s : set)
                         System.out.println(set);
          
               }
          }
          • 2. Re: TreeSet question
            807599
            A note:
            Even though my fix will allow you to compile your code, it is not going to do what you want it to do. You're only inputting one string... think about that for a little bit.
            • 3. Re: TreeSet question
              807599
              oh my God. sorry bro. that is messed up. how will it compile when i dont have a main method. Duh !.
              ok i know its only adding the first word but not taking care of the white space i got two options either use System.getProperty("line.separator") or use the hasNext method of either StringTokenizer or Scanner. let me see if i can achieve it. any clues will be great.
              • 4. Re: TreeSet question
                807599
                it is still displaying the first word. if i had to do with a regular String i could have used the substring method and created a new string. but with this TreeSet i am not able to achieve my goal.
                import java.util.*;
                public class SpliExample {
                     public static void main(String[] args) {
                     Scanner scanner = new Scanner(System.in);
                     String input;
                     Set<String> set = new TreeSet<String>();
                     System.out.println("Enter the word ");
                     input = scanner.next();
                     StringTokenizer tokenizer = new StringTokenizer(input);
                     while(tokenizer.hasMoreTokens()) {
                          String word = tokenizer.nextToken();
                     set.add(input);
                     }
                     for(String s : set)
                          System.out.println(s);
                  }
                  System.out.println("The size is " + set.size());
                }
                • 5. Re: TreeSet question
                  807599
                  input = scanner.next();
                  System.out.println(input); // add this line
                  StringTokenizer tokenizer = new StringTokenizer(input);
                  Perhaps you should read Scanner in the API.
                  • 6. Re: TreeSet question
                    807599
                    no flounder. i know how to print. what i am trying to do over here is create a TreeSet and ask the user to enter a word. like for eg
                    hi hi how are you.
                    now i would like to add this word in the treeset and display it in each line like
                    hi
                    how
                    are
                    you
                    size = 4.
                    as you can see the power of TreeSet which will not accept duplicates. hope you understand my problem now
                    • 7. Re: TreeSet question
                      807599
                      I understand exactly what your problem is. If you had added that line like I suggested, perhaps you might have realised where you are going wrong.
                      • 8. Re: TreeSet question
                        807599
                        ok i realized it was not accepting the words after space so i used delimiter now when i print it prints the whole word but when i try to add the input in my treeSet it still gives me size of 1 and output the first word.
                        import java.util.*;
                        public class SpliExample {
                             public static void main(String[] args) {
                             Scanner scanner = new Scanner(System.in);
                             scanner.useDelimiter(System.getProperty("line.separator"));
                             String input;
                             Set<String> set = new TreeSet<String>();
                             System.out.println("Enter the word ");
                             input = scanner.nextLine();
                             System.out.println(input);
                             StringTokenizer tokenizer = new StringTokenizer(input);
                             while(tokenizer.hasMoreTokens()) {
                                  String word = tokenizer.nextToken();
                             set.add(input);
                             }
                             for(String s : set)
                                  System.out.println(s);
                                  System.out.println("The size is " + set.size());
                          }
                          
                        }
                        • 9. Re: TreeSet question
                          807599
                          yay i got it . Thankyou mr flounder. you are a lifesaver
                          import java.util.*;
                          public class SpliExample {
                               public static void main(String[] args) {
                               Scanner scanner = new Scanner(System.in);
                               scanner.useDelimiter(System.getProperty("line.separator"));
                               String input;
                               Set<String> set = new TreeSet<String>();
                               System.out.println("Enter the word ");
                               input = scanner.nextLine();
                               System.out.println(input);
                               StringTokenizer tokenizer = new StringTokenizer(input);
                               while(tokenizer.hasMoreTokens()) {
                                    String word = tokenizer.nextToken();
                               set.add(word);
                               }
                               for(String s : set)
                                    System.out.println(s);
                                    System.out.println("The size is " + set.size());
                            }
                            
                          }