Can somebody help me, i want to save the output of this program in a .csv file but i don't know how. please i need help.

import java.io.*;

public class TestEmployee
{
    public static void main(String args[]) throws IOException
    {

        Employee staff[] = new Employee[args.length];
        // populating the array of Employee
        for (int i = 0; i < args.length; i++)
        {
            String values[] = args[i].split(",");

            if (values.length == 4)
            {
                staff[i] = new Manager(values[0], values[1], Double.parseDouble(values[2]), values[3]);
            }
            else if (values.length == 3)
            {
                if (Character.isDigit(values[2].charAt(0)))
                    staff[i] = new Employee(values[0], values[1], Double.parseDouble(values[2]));
                else
                    staff[i] = new Manager(values[0], values[1], values[2]);
            }
            else if (values.length == 2)
            {
                staff[i] = new Employee(values[0], values[1]);
            }
            else
            {}
        }
        // printing of the Employee array while computing
        for (int i=0; i<staff.length; i++)
        {
            String values[]=args[i].split(",");
            for(int a=0 ; a<values.length ; a++)
            {
                System.out.println("[Firstname Length:   "+values[0].length()+" ]"
                +"[Lastname Length: "+values[1].length()+" ]"
                +"[Employee IDnumber: "+(i+1)+" ]"+" = "+ staff[i]);
                a++;
            }
        }
    }
}

The Employee and Manager.java is the main class.

Thank you in advance.

Edited 3 Years Ago by Dani: Formatting fixed

Attachments
public class Employee
{
	public static final double BASIC_SALARY = 15000;
	private String lastName;
	private String firstName;
	private double salary;
	
	// constructors
	public Employee(String lastName, String firstName, double salary)
	{
		this.lastName = lastName;
		this.firstName = firstName;
		if (salary >= 0)
			this.salary = salary;
		else
			this.salary = 0;
	}
	public Employee(String lastName, String firstName)
	{
		this(lastName, firstName, BASIC_SALARY);
	}
	// setters
	public void setLastName(String lastName)
	{
		this.lastName = lastName;
	}
	public void setFirstName(String firstName)
	{
		this.firstName = firstName;
	}
	public void setSalary(double salary)
	{
		if (salary >= 0)
			this.salary = salary;
		else
			this.salary = 0;
	}
	// getters
	public String getLastName()
	{
		return lastName;
	}
	public String getFirstName()
	{
		return firstName;
	}
	public double getSalary()
	{
		return salary;
	}
	public String toString()
	{
		return getLastName() + ", " + getFirstName() + " with a salary of " + getSalary();
	}
}
public class Manager extends Employee
{
	private String department;
	// constructors
	public Manager(String lastName, String firstName, 
					double salary, String department)
	{
		super(lastName, firstName, salary);
		this.department = department;
	}
	public Manager(String lastName, String firstName, 
				   String department)
	{
		// super(lastName, firstName);
		// this.department = department;
		this(lastName, firstName, BASIC_SALARY, department);
	}
	// setter
	public void setDepartment(String department)
	{
		this.department = department;
	}
	
	// getter
	public String getDepartment()
	{
		return department;
	}
	public String toString()
	{
		return super.toString() + " of " + getDepartment() + " department.";
	}
}

(1)Create a String object, for instance s (e.g., String s="";) to accommodate the information obtained from your for loop:
for(int a=0 ; a<values.length ; a++){
System.out.println("[Firstname Length: "+values[0].length()+" ]"
+"[Lastname Length: "+values[1].length()+" ]"
+"[Employee IDnumber: "+(i+1)+" ]"+" = "+ staff);
a++;
}
(2)Create a BufferedWriter bw in the following way:
BufferedWriter bw = new BufferedWriter(new FileWriter("a .csv"));
(3)use the member method write(String s) of bw to write the information into the file "a .csv";
bw.write(s);
(4) close the BufferedWriter
bw.close();

Edited 6 Years Ago by tong1: n/a

This question has already been answered. Start a new discussion instead.