Search code examples
javaiostackbufferedwriter

Stack values not being read into file


I'm currently working on the following method:

public void addExpenditure(Stack<Double> costStack){

    try(BufferedWriter bw = new BufferedWriter(new FileWriter(f.getPath(), true))) {

        while (costStack.size() != 0)
            bw.write(costStack.pop().toString() + " ,");

    }catch(Exception e){
        e.printStackTrace();
    }

}

The method is to simply take the values stored in the stack costStack and write them into a file. For an unknown reason, however, this is not happening.

Instead, nothing is written to the file each time. This is strange because this method does exactly as it should, and it is nearly identical:

public void addExpenditure(double amount){

    Double amount1 = amount;

    try(BufferedWriter bf = new BufferedWriter(new FileWriter(f.getPath(), true))){
        bf.write(amount1.toString() + " , ");
    }catch(IOException e){
        e.printStackTrace();
    }

}

No exceptions are thrown either.

Here are all the methods that I am currently using to make this "work":

import java.util.Scanner;
import java.util.Stack;

public class BudgetMain {

/*
Pre-Tax income per month: $5875
After-Tax income per month: $4112
Savings Goal per month (61% of After-Tax income): $2508.62
Expendable income per month (After-Tax income - Savings Goal Per month): $1604
 */

public static void main(String[] args){

    try {
        Budget b = new Budget();
        b.clearFile();
        System.out.println("Available funds left to spend this month: " + b.availableFundsLeft());
        Stack<Double> costStack = new Stack<>();
        costStack = takeValues(costStack);
        b.addExpenditure(costStack);
        System.out.println("Available funds left to spend this month: " + b.availableFundsLeft());
    }catch(Exception e){e.printStackTrace();}

    }


public static Stack<Double> takeValues(Stack<Double> costStack){

    System.out.println("Enter appropriate expenditures");
    Scanner stdin = new Scanner(System.in);

    while(true) {
        costStack.push(stdin.nextDouble());
        if (costStack.peek() == -1){
            costStack.pop();
            return costStack;
        }
    }


}

}

Different class file:

public void addExpenditure(Stack<Double> costStack){

    try(BufferedWriter bw = new BufferedWriter(new FileWriter(f.getPath(), true))) {

        while (costStack.size() != 0)
            bw.write(costStack.pop().toString() + " ,");

    }catch(Exception e){
        e.printStackTrace();
    }

}

public double availableFundsLeft(){
    try{return afterTaxIncome - savingsGoalPerMonth - 
getTotalExpenditureThisMonth();}
    catch(Exception e){e.printStackTrace();}
    return -1;
}

Solution

  • This code seems to work, it creates a test.out file with content 2.0 ,1.0 ,

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.Stack;
    
    public class BudgetMain {
    
    
        public static void main(String[] args){
            Stack<Double> stack = new Stack<>();
            stack.push(1.0);
            stack.push(2.0);
            addExpenditure1(stack, new File("test.out"));
        }
    
    
        public static void addExpenditure1(Stack<Double> costStack, File f){
    
            try(BufferedWriter bw = new BufferedWriter(new FileWriter(f.getPath(), true))) {
    
                while (costStack.size() != 0)
                    bw.write(costStack.pop().toString() + " ,");
    
            }catch(Exception e){
                e.printStackTrace();
            }
    
        }
    }