gpt4 book ai didi

java - 堆栈值未读入文件

转载 作者:行者123 更新时间:2023-12-02 09:48:00 27 4
gpt4 key购买 nike

我目前正在研究以下方法:

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

}

方法是简单地取出存储在堆栈costStack中的值并将它们写入文件中。然而,由于未知的原因,这并没有发生。

相反,每次都不会向文件写入任何内容。这很奇怪,因为这个方法完全按照其应有的方式执行,并且几乎相同:

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

}

也不会抛出任何异常。

以下是我目前用于实现此“工作”的所有方法:

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;
}
}


}

}

不同的类文件:

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;
}

最佳答案

这段代码似乎有效,它创建了一个内容为 2.0 ,1.0 的 test.out 文件,

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

}
}

关于java - 堆栈值未读入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56517746/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com