1 2 3 Previous Next 44 Replies Latest reply on Aug 31, 2007 9:14 PM by 807605 Go to original post
      • 15. Re: Help with Sorting with Comparator
        darrylburke
        TIme enough to worry about parsing the String to an int.

        -- first sort out the loop
        -- then refresh your concept of instanceof

        That code block will never execute anyhow with the if... instance of...

        db
        • 16. Re: Help with Sorting with Comparator
          807605
          thanx for the help so far but i cant figure out how to strip the first quote in the date. this is the error i get now;

          java.lang.NumberFormatException: For input string: ""2007-08-15 01:26:16"
          • 17. Re: Help with Sorting with Comparator
            darrylburke
            Have you understood that you have been processing the fields before looping through all of them?

            And have you corrected that?

            db
            • 18. Re: Help with Sorting with Comparator
              807605
              yea. actually i just realized that i should probably sort the data first and make sure it prints with the specified order described
              • 19. Re: Help with Sorting with Comparator
                807605
                plz ignore the last comment
                • 20. Re: Help with Sorting with Comparator
                  darrylburke
                  I suggest you correct the position of your closing brace for the for loop and re-post your code (use spaces, equal indents and outdents)

                  db

                  edit Last comment ignored on request :-)
                  Message was edited by:
                  Darryl.Burke
                  • 21. Re: Help with Sorting with Comparator
                    807605
                    so this is what it looks like so far. still throwing NumberFormatEx for the date field.
                    import java.io.BufferedReader;
                    import java.io.File;
                    import java.io.FileNotFoundException;
                    import java.io.FileReader;
                    import java.io.IOException;
                    import java.text.DecimalFormat;
                    import java.text.ParseException;
                    import java.text.SimpleDateFormat;
                    import java.util.Collections;
                    import java.util.Comparator;
                    import java.util.Date;
                    import java.util.Vector;
                     
                    public class CODEsort {
                            java.text.DecimalFormat df = new java.text.DecimalFormat("####");
                         public CODEsort() {
                              super();
                              // TODO Auto-generated constructor stub
                         }
                    
                    
                         /**
                          * @param args
                          * @throws ParseException 
                          */
                         public static void main(String[] args) throws ParseException {
                              // TODO Auto-generated method stub
                                        
                              try {
                             BufferedReader br = new BufferedReader(new FileReader(new File("Location of file")));
                             String logLine = "";
                             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                             SimpleDateFormat sdfDate = new SimpleDateFormat("d-MMM");
                             SimpleDateFormat sdfTime = new SimpleDateFormat("H:mm");
                             sdf.setLenient(false);
                             DecimalFormat df = new DecimalFormat("0000");
                             Vector v = new Vector();
                                   
                            {
                                  while((logLine = br.readLine())!= null){
                                    String fields[] = logLine.split(",");
                                    for (int i = 0; i < fields.length; i += 1) {
                                    while (fields.startsWith("\"")) {
                                                  fields[i] = fields[i].substring(1);               
                    }
                    while (fields[i].endsWith("\"")) {
                                                  fields[i] = fields[i].substring(0, fields[i].length() - 1);
                    }
                    }
                    String ts = fields[0].substring(fields[0].indexOf(":")+1);
                    ts = ts.substring(ts.indexOf("\"")+1);
                    Date temp = sdf.parse(ts);
                    //String date = sdfDate.format(temp);
                    //String time = (sdfTime.format(temp));
                                   
                    System.out.println("13 and 14 -> [" +fields[13] + fields[14] + "]");



                                             int tempCode = Integer.parseInt(fields[13] + fields[14]);
                                             String rrdd = df.format(tempCode);
                                             Summary s = new Summary();
                                                  s.setTime(ts);
                                                  s.setCODE(code);
                                                  v.add(s);
                                   
                                             
                         Collections.sort(v, new Sort());
                                   
                         //System.out.println(v);
                         System.out.print(ts);
                         
                         }
                    }
                    }
                         catch (FileNotFoundException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                         }
                         catch (IOException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                         }
                              

                         }
                         
                    }

                    class Summary
                    {
                         String CODE;
                         String Time;
                         
                         public String getCODE() {
                              return CODE;
                         }
                         public void setCODE(String tempCode) {
                              this.CODE = tempCode;
                         }
                         public String getTime() {
                              return Time;
                         }     
                         public void setTime(String Time) {
                              this.Time = Time;
                         }
                         public String toString()
                         {
                              return this.getCODE()+ "," + this.getTime();
                         }
                         
                         
                    }

                    class Sort implements Comparator
                    {

                         public int compare(Object CODE, Object Time) {
                              if(CODE instanceof Summary & Time instanceof Summary)
                         {
                              Summary p1=(Summary)CODE;
                              Summary p2=(Summary)Time;
                              
                              int pi1=Integer.parseInt(p1.getCODE());
                              int pi2=Integer.parseInt(p2.getTime());
                              if(pi1>pi2)
                              return 1;
                              else if(pi1<pi2)
                              return -1;
                         else
                              return 0;
                                        
                    }
                              return 0;
                    }
                         
                    }

                    • 22. Re: Help with Sorting with Comparator
                      807605
                      What does your input file look like? I saw one line of input earlier in this thread:
                      LogName1.log.20070815:"2007-08-30 01:13:17,812","VALID","0","0","262","12","13"
                      but it didn't work when I tried making a small file with this and having your code read it in. Is the input file very large? Is every line formatted the same as above with eight different strings separated by commas?

                      [edit]
                      You do know that if you take the line above and split it with
                      String.split(",");
                      that the split method won't care about quotes and will split the fragment:
                      LogName1.log.20070815:"2007-08-30 01:13:17,812",
                      into this:
                      LogName1.log.20070815:"2007-08-30 01:13:17
                      and this:
                      812"
                      Message was edited by:
                      petes1234
                      • 23. Re: Help with Sorting with Comparator
                        darrylburke
                        Hey, I'm starting to get angry now (not really angry -- but irritated, certainly). What prevents you from posting properly indented code, indented with spaces not tabs, do you really expect people to read your code with closing braces hanging out just anywhere?

                        This is the kind of formatting that makes code readable.
                        import java.io.BufferedReader;
                        import java.io.File;
                        import java.io.FileNotFoundException;
                        import java.io.FileReader;
                        import java.io.IOException;
                        import java.text.DecimalFormat;
                        import java.text.ParseException;
                        import java.text.SimpleDateFormat;
                        import java.util.Collections;
                        import java.util.Comparator;
                        import java.util.Date;
                        import java.util.Vector;
                        
                        public class CODEsort {
                            java.text.DecimalFormat df = new java.text.DecimalFormat("####");
                            public CODEsort() {
                                super();
                                // TODO Auto-generated constructor stub
                            }
                            
                            
                            /**
                             * @param args
                             * @throws ParseException
                             */
                            public static void main(String[] args) throws ParseException {
                                // TODO Auto-generated method stub
                                
                                try {
                                    BufferedReader br = new BufferedReader(new FileReader(new File("Location of file")));
                                    String logLine = "";
                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    SimpleDateFormat sdfDate = new SimpleDateFormat("d-MMM");
                                    SimpleDateFormat sdfTime = new SimpleDateFormat("H:mm");
                                    sdf.setLenient(false);
                                    DecimalFormat df = new DecimalFormat("0000");
                                    Vector v = new Vector();
                                    
                                    {
                                        while((logLine = br.readLine())!= null){
                                            String fields[] = logLine.split(",");
                                            for (int i = 0; i < fields.length; i += 1) {
                                                while (fields.startsWith("\"")) {
                        fields[i] = fields[i].substring(1);
                        }
                        while (fields[i].endsWith("\"")) {
                        fields[i] = fields[i].substring(0, fields[i].length() - 1);
                        }
                        }
                        String ts = fields[0].substring(fields[0].indexOf(":")+1);
                        ts = ts.substring(ts.indexOf("\"")+1);
                        Date temp = sdf.parse(ts);
                        //String date = sdfDate.format(temp);
                        //String time = (sdfTime.format(temp));

                        System.out.println("13 and 14 -> [" +fields[13] + fields[14] + "]");

                        int tempCode = Integer.parseInt(fields[13] + fields[14]);
                        String rrdd = df.format(tempCode);
                        Summary s = new Summary();
                        s.setTime(ts);
                        s.setCODE(code);
                        v.add(s);

                        Collections.sort(v, new Sort());

                        //System.out.println(v);
                        System.out.print(ts);
                        }
                        }
                        } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        }
                        }

                        }So one thing's sorted out, what's next? What error are you getting now?

                        db

                        edit removed some unsightly blank lines
                        Message was edited by:
                        Darryl.Burke
                        • 24. Re: Help with Sorting with Comparator
                          darrylburke
                          Hello Pete, you have sharp eyes.... with my monitor size and resolution I hadn't noticed the euro decimal separator. I'm sure you'll be around, but I'm turning in -- 3 am here :D

                          @OP: I think (actually I'm sure) you're confused between instance variable and instanceof operator. Read
                          http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op2.html

                          Then sort out your wrong usage of if ... instanceof and put something meaningful in its place.

                          Going to bed now, Darryl
                          • 25. Re: Help with Sorting with Comparator
                            807605
                            I did some more modification to the code and now im getting an illegalArgumentEx. I formatted properly too
                            import java.io.BufferedReader;
                            import java.io.File;
                            import java.io.FileNotFoundException;
                            import java.io.FileReader;
                            import java.io.IOException;
                            import java.text.DecimalFormat;
                            import java.text.ParseException;
                            import java.text.SimpleDateFormat;
                            import java.util.Collections;
                            import java.util.Comparator;
                            import java.util.Date;
                            import java.util.Vector;
                            import java.text.DateFormat;
                            
                            
                            public class CODEsort 
                            {
                                 java.text.DecimalFormat df = new java.text.DecimalFormat("####");
                                 public CODEsort() 
                                 {
                                      super();
                                      // TODO Auto-generated constructor stub
                                 }
                            
                            
                                 /**
                                  * @param args
                                  * @throws ParseException 
                                  */
                                 public static void main(String[] args) throws ParseException
                                 {
                                      // TODO Auto-generated method stub
                                      Vector v;
                                      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                      try 
                                      {
                                           BufferedReader br = new BufferedReader(new FileReader(new File("FILE SOURCE")));
                                           String logLine = "";
                                           
                                           SimpleDateFormat sdfDate = new SimpleDateFormat("d-MMM");
                                           SimpleDateFormat sdfTime = new SimpleDateFormat("H:mm");
                                           sdf.setLenient(false);
                                           DecimalFormat df = new DecimalFormat("0000");
                                            v = new Vector();
                                           
                                      {
                                           while((logLine = br.readLine())!= null)
                                           {
                                                String fields[] = logLine.split(",");
                                                for (int i = 0; i < fields.length; i += 1) 
                                                {
                                                     while (fields.startsWith("\"")) 
                                                     {
                                                          fields[i] = fields[i].substring(1);
                                                     }
                                                     while (fields[i].endsWith("\""))
                                                     {
                                                          fields[i] = fields[i].substring(0, fields[i].length() - 1);
                                                     }
                                                }
                                                String ts = fields[0].substring(fields[0].indexOf(":")+1);
                                                ts = ts.substring(ts.indexOf("\"")+1);
                                                Date temp = sdf.parse(ts);
                                                //String date = sdfDate.format(temp);
                                                //String time = (sdfTime.format(temp));
                                           
                                                //System.out.println("13 and 14 -> [" +fields[13] + fields[14] + "]");
                                                //System.out.println("kdkdf:" + fields[0].substring(fields[0].indexOf(":")+1));


                                                          int tempCode = Integer.parseInt(fields[13] + fields[14]);
                                                          String code = df.format(tempCode);
                                                          Summary s = new Summary();
                                                          s.setTime(ts);
                                                          s.setCODE(code);
                                                          v.add(s);
                                           
                                                     

                                           //System.out.print(ts);
                                           //System.out.println(fields[0]);
                                           
                                           }
                                      }
                                           Collections.sort(v, new Sort());
                                           
                                           System.out.println(v);     
                                           
                                 }
                                      catch (FileNotFoundException e)
                                      {
                                           // TODO Auto-generated catch block
                                           e.printStackTrace();
                                      }
                                      catch (IOException e)
                                      {
                                           // TODO Auto-generated catch block
                                           e.printStackTrace();
                                      }
                                      

                                 }
                                 
                            }

                            class Summary
                                 {
                                      String CODE;
                                      String Time;
                                      
                                      public String getCODE()
                                      {
                                           return CODE;
                                      }
                                      public void setCODE(String tempCode)
                                      {
                                           this.CODE = tempCode;
                                      }
                                      public String getTime()
                                      {
                                           return Time;
                                      }     
                                      public void setTime(String Time)
                                      {
                                           this.Time = Time;
                                      }
                                      public String toString()
                                      {
                                           return this.getCODE()+ "," + this.getTime();
                                      }
                                      
                                      
                                 }
                                 
                            class Sort implements Comparator
                            {

                                 public int compare(Object summ1, Object summ2)
                                 {
                                      if(summ1 instanceof Summary & summ2 instanceof Summary)
                                      {
                                           Summary p1=(Summary)summ1;
                                           Summary p2=(Summary)summ2;
                                           
                                           int rrdd1 = Integer.parseInt(p1.getCODE());
                                           int rrdd2 = Integer.parseInt(p2.getCODE());
                                           
                                           if (code1 < code2)
                                           {
                                                return 1;
                                           }
                                           else
                                           {
                                                if (code1 > code2)
                                                {
                                                     return 2;
                                                }
                                                else
                                                {
                                                     String temp =p1.getTime() ;
                                                     long date1 = Date.parse(temp);
                                                     long date2 = Date.parse(p2.getTime());
                                                     
                                                     if (date1 < date2)
                                                     {
                                                          return 1;
                                                     }
                                                     else
                                                     {
                                                          if (date1 > date2)
                                                          {
                                                               return -1;
                                                          }
                                                     }
                                                     
                                                     
                                                }
                                           }
                                           
                                           

                                 }
                                      return 0;
                                 
                            }
                            }

                            • 26. Re: Help with Sorting with Comparator
                              darrylburke
                              See #24. Follow the link and learn the difference between an instance variable and instanceof operator.

                              And your code formatting is better than it was earlier, but the last 2 braces aren't lined up properly. See #23 -- if you use 2 to 4 spaces rather than tabs, your code will be much easier to read on this forum.

                              Come back when you've read up on instanceof.

                              db

                              edit Why is there an opening brace on the line before the start of your while loop, closed one line after the end of the same while loop? The while loop is already a code block, what possible reason can you have for wanting to enclose only the hwile loop and nothing else in a sirrounding code block?

                              Get rid of those unnecessary braces and adjust the code indents accordingly.

                              I'll get back each time I spot something more, you read up instanceof

                              Message was edited by:
                              Darryl.Burke
                              • 27. Re: Help with Sorting with Comparator
                                807605
                                it really doesnt explain anything. that shouldnt be a reason for the IllegalArgs exception should it?
                                • 28. Re: Help with Sorting with Comparator
                                  807605
                                  Without a working example of the data file, who knows?
                                  • 29. Re: Help with Sorting with Comparator
                                    807605
                                    sry. Here is a complete line.
                                    LogName1.log.20070815:"2007-08-30 01:13:17,812"," VALID","text","131","Log1","RUN","3","1","Event","Class","262","12","13","","","","2007-08-15","","","","","","","processed","","","",""
                                    
                                    LogName1.log.20070815:"2007-08-30 01:36:17,439"," VALID","text","131","Log1","RUN","0","1","Event","Class","260","01","13","","","","2007-08-15","","","","","","","processed","","","",""
                                    the error is pointing to these lines
                                    long date1 = Date.parse(temp); 
                                    
                                    Collections.sort(v, new Sort());