1 2 3 Previous Next 44 Replies Latest reply: Aug 30, 2007 4:31 PM by DarrylBurke RSS

    Help with Sorting with Comparator

    807605
      Can someone plz help me debug. idk why im getting NumberFormatException here. im trying to sort the data by CODE first then by Time. Here is my code. thanks in advance
      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) {
                               //this just strips the quotes 
                               while (fields.startsWith("\"")) {
                                    fields[i] = fields[i].substring(1);
                               }
                               while (fields[i].endsWith("\"")) {
                                    fields[i] = fields[i].substring(0, fields[i].length() - 1);
                               }
                               String tempDate = fields[0].substring(fields[0].indexOf(":")+1);
                               tempDate = tempDate.substring(tempDate.indexOf("\"")+1);
                               //Date temp = sdf.parse(tempDate);
                               //String date = sdfDate.format(temp);
                               //String time = (sdfTime.format(temp));
                                              
                               {
                                    //ts is the time
                                    String ts = fields[0].substring(fields[0].indexOf(":")+1);
                                    //field that has the codes
                                    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);
                     
                               
                     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 CODE, Object Time) {
                if(CODE instanceof Summary & Time instanceof Summary)
                {
                     Summary p1=(Summary)CODE;
                     Summary p2=(Summary)Time;
                     String pi1=(p1.getCODE());
                     int pi2=Integer.parseInt(p2.getTime());
                     if(pi1>pi2)
                          return 1;
                     else if(pi1<pi2)
                          return -1;
                     else
                          return 0;
                          
                }
                return 0;
           }
           
      }
        • 1. Re: Help with Sorting with Comparator
          807605
          NumberFormatException means you called Integer.parseInt/Double.parseDouble() or some other number parsing routine and the string you used was not a number

          Post the string you are trying to parse and we'll tell you why it's not a number
          • 2. Re: Help with Sorting with Comparator
            DarrylBurke
            It would help if you told us which code line is throwing the NumberFormatException.

            At a glance, I wonder whether in this line
            int tempCode = Integer.parseInt(fields[13] + fields[14]);
            fields[13] + fields[14] might evaluate to a null string or blank?

            Just a guess, Darryl
            • 3. Re: Help with Sorting with Comparator
              807605
              Here is one of the lines that im trying to parse
              LogName1.log.20070815:"2007-08-30 01:13:17,812","VALID","0","0","262","12","13"
              the error is pointing to the field with "12","13". Thas the tempCode field

              null
              • 4. Re: Help with Sorting with Comparator
                807605
                Here is one of the lines that im trying to parse
                LogName1.log.20070815:"2007-08-30
                01:13:17,812","VALID","0","0","262","12","13"
                the error is pointing to the field with "12","13".
                Thas the tempCode field

                null
                Stick this:
                System.out.println("13 and 14 -> [" +fields[13] + fields[14] + "]");
                Just before this:
                int tempCode = Integer.parseInt(fields[13] + fields[14]);
                And check out what it's output is.
                • 5. Re: Help with Sorting with Comparator
                  807605
                  output: 13 and 14 -> ["12""13"]
                  • 6. Re: Help with Sorting with Comparator
                    807605
                    The String that you're parsing into an int is between the [] in that debug statement. The String has double quotes in it, which is why it fails. Strip the " from those two strings before you parse them.
                    • 7. Re: Help with Sorting with Comparator
                      807605
                                {
                                     Summary p1=(Summary)CODE;
                                     Summary p2=(Summary)Time;
                                     String pi1=(p1.getCODE());
                                     int pi2=Integer.parseInt(p2.getTime());
                                     if(pi1>pi2)
                                          return 1;
                                     else if(pi1<pi2)
                                          return -1;
                                     else 
                                          return 0;
                                          
                                }
                      Note:
                      String pi1=(p1.getCODE());
                      and
                      if(pi1[B] >  pi2)

                      You cannot compare a String (greater than or less than) with an integer !
                      • 8. Re: Help with Sorting with Comparator
                        DarrylBurke
                        This is your for loop:
                            for (int i = 0; i < fields.length; i += 1)
                            {
                               //this just strips the quotes 
                               while (fields.startsWith("\""))
                        {
                        fields[i] = fields[i].substring(1);
                        }
                        while (fields[i].endsWith("\""))
                        {
                        fields[i] = fields[i].substring(0, fields[i].length() - 1);
                        }
                        String tempDate = fields[0].substring(fields[0].indexOf(":")+1);
                        tempDate = tempDate.substring(tempDate.indexOf("\"")+1);
                        //Date temp = sdf.parse(tempDate);
                        //String date = sdfDate.format(temp);
                        //String time = (sdfTime.format(temp));
                                                                
                        {
                        //ts is the time
                        String ts = fields[0].substring(fields[0].indexOf(":")+1);
                        //field that has the codes
                        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);
                             
                        Collections.sort(v, new Sort());
                                       
                        System.out.println(v);
                        }
                        }
                        Now, do you really want to start processing the individual fields before you've looped through all of them, stripping the quotes? Do you really want to run all those code lines in every iteration of the loop?

                        You have made this mistake because of not maintaining proper indentation. Notice your line with 4 closing braces:
                                  } }}}
                        Balance your indents and outdents. put closing braces on a new line and you'll never miss something like this again!

                        db
                        • 9. Re: Help with Sorting with Comparator
                          807605
                          i stripped the quotes earlier
                          • 10. Re: Help with Sorting with Comparator
                            DarrylBurke
                            Also... use spaces rather than tabs for indentation, the forum software doesn't handle tabs very well.

                            db
                            • 11. Re: Help with Sorting with Comparator
                              DarrylBurke
                              i stripped the quotes earlier
                              See #8
                              • 12. Re: Help with Sorting with Comparator
                                807605
                                          {
                                               Summary p1=(Summary)CODE;
                                               Summary p2=(Summary)Time;
                                               String pi1=(p1.getCODE());
                                               int pi2=Integer.parseInt(p2.getTime());
                                               if(pi1>pi2)
                                                    return 1;
                                               else if(pi1<pi2)
                                                    return -1;
                                               else 
                                                    return 0;
                                                    
                                          }
                                Note:
                                String pi1=(p1.getCODE());
                                and
                                if(pi1[B] >  pi2)
                                You cannot compare a String (greater than or less
                                than) with an integer !
                                so how would i go about comparing these two diff types?
                                • 13. Re: Help with Sorting with Comparator
                                  DarrylBurke
                                  so how would i go about comparing these two
                                  diff types?
                                  The same way, using Integer.parseInt(...) to convert the String to int and then comparing.

                                  But sort out your for loop first. One thing at a time.

                                  db
                                  • 14. Re: Help with Sorting with Comparator
                                    807605
                                    Integer.parseInt (pi1);
                                    1 2 3 Previous Next