0

我正在制作一个简单的程序,我可以在其中跟踪我的储蓄。我已经包括了周数、收入和保存的字段。我将此数据保存到文本文件中,然后可以将其检索到文本区域。关闭程序然后再次运行它时,如何跟踪我的运行总数“总数”。我使用了一个数组作为运行总计,但是当我杀死它然后重新打开它(显然)时,这会重新开始。

这是我的保存和阅读代码。总而言之,我希望能够将最后一个字段“总计”作为变量检索,以便可以将其添加到新输入中。

    //get week and validate:
    strWeek = txtWeek.getText();
    if (strWeek.isEmpty())
    {
        JOptionPane.showMessageDialog(null, "Enter Week Number");
        return;
    }
    else
        week = Integer.parseInt(strWeek);

    //get income and validate:
    strIncome = txtIncome.getText();
    if (strIncome.isEmpty())
    {
        JOptionPane.showMessageDialog(null, "Enter Income");
        return;
    }
    else
        income = Double.parseDouble(strIncome);

    //get saved:
    strSaved = txtSaved.getText();
    if (strSaved.isEmpty())
    {
        JOptionPane.showMessageDialog(null, "Enter Saved");
        return;
    }
    else
        saved = Double.parseDouble(strSaved);

    total = total + saved;

    txtOutput.append(week + "\t" + income + "\t" + saved + "\t" + total + "\n");
    txtWeek.setText(null);
    txtIncome.setText(null);
    txtSaved.setText(null);

    try
    {
        File output = new File("C:\\savingsApp/Savings.txt");
        BufferedWriter outFile = new BufferedWriter (new FileWriter(output.getPath(), true));
        outFile.write(week + "\t" + income + "\t" + saved + "\t" + total);
        outFile.newLine();
        outFile.close();
        JOptionPane.showMessageDialog(null, "Savings updated"); 
    }

    catch (Exception e)
    {
        JOptionPane.showMessageDialog(null, "IO file error");
    }

 txtOutput.setText(null);
    //Declare:
    String incomingString = "";
    int counter = 0;
    //get file:
    try
    {
        File inputFile = new File ("C:\\savingsApp/savings.txt");
        BufferedReader inFile = new BufferedReader (new FileReader(inputFile));
        incomingString = inFile.readLine();
        while (incomingString != null)
        {
            counter++;
            txtOutput.append(incomingString + "\n");
            incomingString = inFile.readLine();
        }

    }
    catch (Exception e)
    {
        JOptionPane.showMessageDialog(null, "error loading file");
    } 
4

2 回答 2

0

runningTotal在终止程序之前制作ArrayList并保存到单独的文件中。这可以是逗号(或制表符)分隔的形式。例如runningTotal.csv可能看起来像这样:

150,200,50,...,300

当您读入时,savings.txt您也可以读入runningTotal.csv,然后将更多总数添加到ArrayList. 再次关闭程序时,更新runningTotal.csv.

于 2013-06-18T16:23:47.810 回答
0

我假设您真正要问的是一种解析从文件中获取的文本的方法。例如,实现此目的的一种方法是使用java.util.Scanner,如下所示:

private Map<String, String> parseData(String text) {
  Map<String, String> data = new HashMap<String, String>();
  Scanner scanner = new Scanner(text);
  data.put("week", scanner.next());
  data.put("income", scanner.next());
  data.put("saved", scanner.next());
  data.put("total", scanner.next());
  return data;
}

然后你可以做这样的事情:

...
while (incomingString != null) {
  counter++;
  String totalStr = parseData(incomingString).get("total");
  txtOutput.append(incomingString + "\n");
  incomingString = inFile.readLine();
}
于 2013-06-19T09:56:32.103 回答