Results 1 to 2 of 2

Thread: Help with Sorting with Comparator

  1. #1
    Junior Member
    Join Date
    Sep 2006
    Answers
    12

    Help with Sorting with Comparator

    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


    Code:
    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 {
     
    	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[i].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;
    	}
    	
    }



  2. #2
    Junior Member
    Join Date
    Jan 2007
    Answers
    22

    Re: Help with Sorting with Comparator

    First of there seems to be a problem with this code:

    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;
    }


    The operator ">" is not defined for the summary objects. are they?


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact