Click here to Skip to main content
15,670,097 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hai,i m able to write data into notepad but not printing into excel please help me to get out of this error
Employee class:
public class Employee  {
										
        private String LeftKey3;
		private String LeftKey2;
		private String RelationTypeUniqueName;
		private String RightKey2;
		private String LeftKey1;
	    private String RightKey1;
	    private String RightKey3;
	    private String RightId;
	    private String LeftId;
	   

	    public String getLeftKey3() {
	        return LeftKey3;
	    }
	    public void setLeftKey3(String LeftKey3) {
			this.LeftKey3 = LeftKey3;
		}
	    public String getLeftKey2() {
	        return LeftKey2;
	    }
	    public void setLeftKey2(String LeftKey2) {
			this.LeftKey2 = LeftKey2;
		}
	    public String getRelationTypeUniqueName() {
	        return RelationTypeUniqueName;
	    }
	    public void setRelationTypeUniqueName(String RelationTypeUniqueName) {
			this.RelationTypeUniqueName =RelationTypeUniqueName;
		}
	    public String getRightKey2() {
	        return RightKey2;
	    }
	    public void setRightKey2(String RightKey2) {
			this.RightKey2 =RightKey2;
		}

	    public  String getLeftKey1() {
	        return LeftKey1;
	    }
	    public void setLeftKey1(String LeftKey1) {
			this.LeftKey1 =LeftKey1;
		}
	    public String getRightKey1() {
	        return RightKey1;
	    }
	    public void setRightKey1(String RightKey1) {
			this.RightKey1 = RightKey1;
		}
	    public String getRightKey3() {
	        return RightKey3;
	    }
	    public void setRightKey3(String RightKey3) {
			this.RightKey3 = RightKey3;
		}
	    public String getRightId() {
	        return RightId;
	    }
	    public void setRightId(String RightId) {
			this.RightId = RightId;
		}
	    public String getLeftId() {
	        return LeftId;
	    }
	    public void setLeftId(String LeftId) {
			this.LeftId= LeftId;
		}
	    
	   /* public Employee(String LeftKey3,String LeftKey2,String RelationTypeUniqueName,String RightKey2,String LeftKey1,String RightKey1,String RightKey3,String RightId,String LeftId) {
	        this.LeftKey3 = LeftKey3;
	        this.LeftKey2 = LeftKey2;
	        this.RelationTypeUniqueName = RelationTypeUniqueName;
	        this.RightKey2 = RightKey2;
	        this.LeftKey1 = LeftKey1;
	        this.RightKey1 = RightKey1;
	        this.RightKey3 = RightKey3;
	        this.RightId = RightId;
	        this.LeftId =LeftId;
	    }
*/
	   
	    

public static Comparator<Employee> employeeNameComparator = new Comparator<Employee>() {
	public int compare(Employee  employee1, Employee  employee2) {

String rightKey1 =  employee1.getRightKey1();
String rightKey2 =  employee2.getRightKey1();
if(rightKey1.contains("|") && rightKey2.contains("|"))
{
	int firstemp= Integer.parseInt(rightKey1.substring(0,rightKey1.indexOf('|')));
	int secondemp= Integer.parseInt(rightKey2.substring(0,rightKey2.indexOf('|')));

	if(firstemp<secondemp)
	{
		return -1;
	}
	else if (firstemp>secondemp)
	{
		return 1;
	} else
	{
		return 0;
	}
}
else
{
	return rightKey1.compareTo( rightKey2);
}

}

	   };	
	    

	  
		
		
	    public String toString() {
	        return "\n" + this.LeftKey3 + ", " + this.LeftKey2+ ", " + this.RelationTypeUniqueName + ", " +
	        		this.RightKey2 + ","+ this.LeftKey1 +","+ this.RightKey1 +","+ this.RightKey3 +","+ this.RightId  +","+ this.LeftId +"";
	    }
		

	}

What I have tried:

<pre> private static final String NEW_LINE_SEPARATOR = "\n";
    private static final String [] VENDORSUBSIDARY_REPLACE= {"LeftKey3","LeftKey2","RelationType.UniqueName","RightKey2","LeftKey1","RightKey1","RightKey3","RightId","LeftId"};
    private static final String [] VENDORCURRENCY_HEADERS={"LeftKey3","LeftKey2","RelationType.UniqueName","RightKey2","LeftKey1","RightKey1","RightKey3","RightId","LeftId"};
     
    public static void main(String args[])throws IOException
    {
    	

        BufferedReader br = null;
        CSVPrinter csvFilePrinter = null;
        FileWriter fileWriter = null;
      
    	
        String replacecurrencycodeFile = "C:/Users/Desktop/newsample.csv";
        try
        {
        	CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);
		    fileWriter = new FileWriter( replacecurrencycodeFile);
		    csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
		    csvFilePrinter.printRecord(VENDORSUBSIDARY_REPLACE);
		           //Reading the csv file
            br = new BufferedReader(new FileReader("C:/Users/Desktop/Internalid1.csv"));
            
            //Create List for holding Employee objects
            List<employee> empList = new ArrayList<employee>();
            
            String line = "";
            //Read to skip the header
            br.readLine();

						
            //Reading from the second line
            while ((line = br.readLine()) != null) 
            {
              String[] employeeDetails = line.split(",");
                
                if(employeeDetails.length != 0 )
                {
                    //Save the employee details in Employee object
                    Employee emp = new Employee(employeeDetails[0], employeeDetails[1],employeeDetails[2],employeeDetails[3],employeeDetails[4],employeeDetails[5],employeeDetails[6],employeeDetails[7],employeeDetails[8]);
                    empList.add(emp);
                }
            }
            Collections.sort(empList,Employee.employeeNameComparator );
            //Lets print the Employee List
            for(Employee e : empList)
            {
               //System.out.println(e.getLeftKey3()+"  "+e.getLeftKey2()+"  "+e.getRelationTypeUniqueName()+" "+e.getRightKey2()+" "+e.getLeftKey1()+" "+e.getRightKey1()+" "+e.getRightKey3()+" "+e.getRightId()+" "+e.getLeftId());
            System.out.println(e);	
            csvFilePrinter.printRecord(e);
           
                		
            }
           
            
            System.out.println("done");
            br.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileWriter.flush();
                fileWriter.close();
                csvFilePrinter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        }
Posted
Updated 20-Jun-18 0:49am
v3
Comments
Jochen Arndt 19-Jun-18 8:13am    
This can't be answered without knowing about the implementation of the Employee class because that defines which CSVPrinter.printRecord() overload is finally used.

[EDIT]
Just noticed that you also use the class name 'employee' for the list but 'Employee' for the values to be added to the list!
[/EDIT]
Member 13746934 20-Jun-18 6:49am    
I have added the employee class data please go through it
Jochen Arndt 20-Jun-18 7:00am    
There are two overloads for the CSVPrinter.printRecord() function where the one accepting an Object applies here and your class contains the required toString() method. But that preceeds the string with a new line what is probably not what you want because printRecord() appends one so that you would have empty lines between records.

However, what is not working?
Do you have compilation errors, runtime errors, or is the output just not as expected?

This should raise a compilation error (as already noted):
List<employee> empList = new ArrayList<employee>();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900