-1

good day guys, im creating a program that will write the data to a text file. the data is coming from an array list, it is a data that is solved from the array list e.g :

    public double getGincome(){
        gincome=rpd*dwork;
        return gincome;
    }

the problem is i cannot write to my txt file..here is my code in writing the data:

public static boolean payrollWriteToFile(String filename) {
        boolean saved = false;
        PrintWriter pw = null; // pw is a PrintWriter identifier

        try {
            // instantiate pw as PrintWriter, FileWriter
            pw = new PrintWriter(new FileWriter(filename)); 

            try {

                // for each loop. each data from payrolls is 
written to parameter

                for (Person payroll : payrolls) {

                    pw.println(payroll.getName());
                    pw.println(payroll.getGincome());
                    pw.println(payroll.getSss());
                    pw.println(payroll.getPagibig());
                    pw.println(payroll.getPhil());
                    pw.println(payroll.getDeduc());
                    pw.println(payroll.getNincome());


                }
                saved = true;
            } finally {
                pw.close();
            }
        } catch (IOException e) {

            e.printStackTrace();
        }
        return saved;
    }

heres my array

public class Person {

private String name;
private String add;
private String gender;
private String status;
    public double rpd;
    public double dwork;
    public static int EmployeeCount;
    public double gincome;
    public double nincome;
    public double deduc;
    public double sss;
    public double pagibig;
    public double phil;



public Person(double gincome, double nincome, double deduc, double sss, double 
pagibig, double phil ) {
    this.gincome = gincome ;
    this.nincome = nincome;
    this.deduc = deduc;
    this.sss = sss;
            this.pagibig= pagibig;
            this.phil = phil;
}

Person( String name , double gincome, double sss, double pagibig, double phil,   
double deduc, double nincome){
    this.gincome = gincome;
    this.nincome = nincome;
    this.sss = sss;
    this.pagibig = pagibig;
    this.phil = phil;
    this.deduc = deduc;

}

Person(String name, String add, String gender, String status, double dwork, double rpd)    
{

            this.name = name;
    this.add = add;
    this.gender = gender;
    this.status = status;
            this.rpd = rpd;
            this.dwork = dwork;

}



    public double getGincome(){
        gincome=rpd*dwork;
        return gincome;
    }

    public double getDeduc(){
        double sss = gincome *.03 ;
        double pagibig = gincome *.02;
        double philhealth = gincome* .0125 ;
        deduc= sss + pagibig +philhealth;

        return deduc;
    }


     public double getNincome(){
         return nincome;
     }

     public double getSss(){
         return sss = getGincome() * .03;


     }
     public double getPagibig(){
         return pagibig = getGincome() * .02;


     }
     public double getPhil(){
         return phil = getGincome() * .0125;


     }
    public static int getEmployeeCount(){
    return EmployeeCount;
   }

public String getName() {
    return name;
}

public String getAdd() {
    return add;
}

public String getGender() {
    return gender;
}

public String getStatus() {
    return status;
}

   public double getRpd(){
            return rpd;
    }

    public double getDwork(){
            return dwork;
    }



public void setName(String name) {
    this.name = name;
}

public void setAdd(String add) {
    this.add = add;
}

public void setGender(String gender) {
    this.gender = gender;
}

public void setStatus(String status) {
    this.status = status;

}

    public void setRpd(double rpd){
        this.rpd = rpd;
    }

    public void setdWork(double dwork){
        this.dwork = dwork;
    }




}

i hope you can help me guys.

NewInJava
  • 35
  • 1
  • 4
  • 12
  • 1
    What do you mean *can not write*? You need to be more specific. Are you encountering errors? If so, What are they? ... What line do they occur at? – Hunter McMillen Apr 03 '13 at 00:02
  • You need to provide more details. Whithout more information I would guess it has to do with write permisions on your file system. – OlivierLi Apr 03 '13 at 00:03
  • why can't you write? what is the error that is thrown ? – jsedano Apr 03 '13 at 00:05
  • actually there is no errors thrown i think the problem is logical because i want to call a data from my array list (not inputted, just calculated there) and write it to my file so i intend to use `pw.println(payroll.getGincome);` "pw" is an object for PrintWriter. – NewInJava Apr 03 '13 at 00:10
  • Writing to file seems to works fine. If I use `pw.println("A");` instead of printing `payroll.someMethod()` "A" is stored in file. If your file is inside some project try to refresh project to see changes. Or maybe you are writing some data to file but they are incorrect (like you wanted to add new data to the end without removing previous data)? Could you tell us more details about problem you are facing? – Pshemo Apr 03 '13 at 00:12
  • @Pshemo but it will be troublesome in the user because if your code is like that in the whole process of the program the only thing that will be printed is "A" what about if the user entered a new values – NewInJava Apr 03 '13 at 00:17
  • @Pshemo that's right i want to add new data without removing the previous one that's why i used array list. – NewInJava Apr 03 '13 at 00:18
  • We are trying to find out where is the problem. Writing "A" to file means that PrintWriter works fine and problem is probably somewhere else. We really need to know how you would like program to behave and how it behaves in reality. – Pshemo Apr 03 '13 at 00:19
  • **cannot write in the file**, the rise of the machines. – Paul Vargas Apr 03 '13 at 00:22
  • in the array list there is the element num1 and num2 the user need to input the values of that, using the set and get method the thing i want to do is in the array list i have this method `public double sum(){ return sum = getnum1() + getnum2(); }` and the return value of the sum is what i want to be written in the txt file – NewInJava Apr 03 '13 at 00:23
  • So it seems that you are reading data from file at start of the program to fill ArrayList. In that case is it possible that you didn't close stream that was used to read them? BTW to be able to append data to file you can use `new PrintWriter(new FileWriter(filename,true));` true enables appending to file. – Pshemo Apr 03 '13 at 00:24
  • @Pshemo `new PrintWriter(new FileWriter(filename,true));` didnt help – NewInJava Apr 03 '13 at 00:35
  • Lets do some test. Replace your `for (Person payroll : payrolls) {...}` loop with `System.out.print("Writing to file... "); pw.print("test value"); System.out.println("done.");` and tell us if your file contains `"test value"` after it. Also on console you should see: `Writing to file... done.`. – Pshemo Apr 03 '13 at 00:43
  • you mean like this? `for (Person payroll : payrolls) { pw.println("test done");}` – NewInJava Apr 03 '13 at 00:50
  • No, don't use `for (Person payroll : payrolls)` since one of the reasons that you didn't write anything to file could be that there wasn't anything to write. It could be possible if `payrolls` was empty. – Pshemo Apr 03 '13 at 00:57
  • i tried that to but nothing was written – NewInJava Apr 03 '13 at 01:02
  • Did you add `System.out.print("Writing to file... ")` to your code? Did console print it? If not then maybe you are not invoking `payrollWriteToFile` at all. – Pshemo Apr 03 '13 at 01:06
  • @Pshemo actually the method "payrollWriteToFile" is not accessed in the console its just simply writing in the file and when i need to call the data i used readfile – NewInJava Apr 03 '13 at 01:19
  • Sorry but without [SSCCE](http://sscce.org/) that I could use to reproduce your problem I won't be able to help you. For now I'm going to sleep. Good luck with your code, I will try to help you tomorrow. – Pshemo Apr 03 '13 at 01:32

2 Answers2

1

Use a BufferedWriter instead, if you are allowed to (maybe you aren't for homework purposes or some other reason).

File file = new File(filename);
if (!file.exists())
{
    try
    {
        file.createNewFile();
    } catch (Exception e)
    {
        e.printStackTrace();
    }
}
try
{
    // Read to end of file for writing
    BufferedReader read = new BufferedReader(new FileReader(file));
    String complete = "";
    String line = null;
    while ((line = read.readLine()) != null)
    {
        complete += line + "\n";
    }

    // Write your data
    BufferedWriter write = new BufferedWriter(new FileWriter(file));
    for (Person payroll : payrolls) 
    {
        write.append(payroll.getName());
        write.append(Double.toString(payroll.getGincome()));
        write.append(Double.toString(payroll.getSss()));
        write.append(Double.toString(payroll.getPagibig()));
        write.append(Double.toString(payroll.getPhil()));
        write.append(Double.toString(payroll.getDeduc()));
        write.append(Double.toString(payroll.getNincome()));
    }
    read.close();
    write.close();
} catch (Exception e)
{
    e.printStackTrace();
}

This should work if your for loop and all your getter methods are properly used.

syb0rg
  • 8,057
  • 9
  • 41
  • 81
0

Try pw.flush(); before pw.close(); in the finally block. It may solve the problem. Otherwise the code looks correct to me.

Joseph Selvaraj
  • 2,225
  • 1
  • 21
  • 21