3 Replies Latest reply: Oct 26, 2009 10:17 AM by 843789 RSS

    How can i remove the integer and the double in the TREESET in java?

    843789
      can anyone help me?
      import java.util.*;
      import java.io.*;
      
      public class assign1
      {
      
           public static void main(String [] args)throws Exception
           {
                
           
                TreeSet oldaccs = new TreeSet();
                TreeSet curraccs = new TreeSet();
                TreeSet newaccs =new TreeSet();
                TreeSet gradaccs = new TreeSet();
                
           try
                {
                     readAccounts("oldAccounts.txt", oldaccs);
                     readAccounts("currAccounts.txt", curraccs);
                }
                catch(IOException ioe)
                {
                     System.out.println("Cannot open file");
                     System.exit(1);
                }
           
                //Newacc
                newaccs.addAll(curraccs); 
                newaccs.removeAll(oldaccs);
                
                PrintWriter p = new PrintWriter(new FileWriter("newAccounts.txt"));
                Iterator it;
                it = newaccs.iterator();
                
                while(it.hasNext()){
                          
                p.println(it.next());
                
                 String str;
                 str="!NA";
                 
                 while(it.equals(str))
                 {
                           p.println(it.next());
                 }
           
                
                }
                p.close();
                
                //gradAcc
                gradaccs.removeAll(curraccs); 
                gradaccs.addAll(oldaccs);
                PrintWriter q = new PrintWriter(new FileWriter("gradAccounts.txt"));
                
                Iterator it2;
                it2 = gradaccs.iterator();
                
                while(it2.hasNext()){
                          
                q.println(it2.next());
                }
                q.close();
      
                
           }
      
           public static void readAccounts(String filename, TreeSet accs) 
                          throws IOException
           {
                Scanner sc = new Scanner(new FileReader(filename));
                String accName;     
                while (sc.hasNext())
                {
                     accName = sc.next();
                     accs.add(accName);
                     
                }
            }
            
            
      }// end of class
      it will auto create a 2 text file and auto write the result to the txt file but i just need the name. Below is the gradAccounts.txt output:
      1.89
      1.98
      2.00
      2.21
      2.33
      2.34
      2.44
      2.49
      2.56
      2.67
      2.76
      2.78
      2.88
      2.89
      3.00
      3.10
      3.23
      3.33
      3.43
      3.44
      3.50
      3.67
      3.76
      3.79
      3.88
      4.00
      Amenda
      Boby
      Brandon
      CGPA
      Cathy
      Clark
      David
      Garry
      ID
      Jacky
      Jasmine
      Jimmy
      Jonh
      Julie
      Kevin
      Laura
      Leo
      Marry
      Michael
      Micheal
      Name
      Nim
      Paula
      Peter
      Rachel
      Rock
      Rosetta
      S0001
      S0008
      S0014
      S0020
      S0023
      S0027
      S0065
      S0076
      S0077
      S0079
      S0092
      S0096
      S0103
      S0105
      S0111
      S0113
      S0120
      S0168
      S0175
      S0184
      S0189
      S0212
      S0260
      S0262
      S0314
      S0317
      S0321
      S0329
      S0406
      S0628
      Sam
      Sharon
      Smith
      Suki
      Susan
      Tom