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.

Recommended Answers

All 2 Replies

(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();

:D thank you sir.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.