尝试解析EDI文件并汇总发票总计

时间:2015-03-30 17:39:16

标签: java parsing edi

我设法提取了我正在寻找的数字但是(可能是因为我从大学开始就没用过Java)我无法添加这些数字来获得一个&#34 ;总"并计算EDI文件。输出显示我将每个发票总数收集为double,但是当我尝试添加它们时,它会在每次while循环运行时重置该数字,如果我将它放在循环之外它不会识别我的变量。

这是我的代码

try (BufferedReader br = new BufferedReader(new FileReader("\\Edi810o311214.dat")))
{
    String line;
    while ((line = br.readLine())!= null)
    { 
    //eliminates lines that do not begin with "total"
    String lineEval = line.substring(0,5);
    if (lineEval.equals("Total"))
    {   
        //extracts total invoice field from line
        String totalValue = line.substring(12,26);
        double totalValueNum = Double.parseDouble(totalValue);

        //shows output for testing purposes (will remove)
        System.out.println(totalValueNum);    
        double total = 0;
        int counter = 0;

        total += totalValueNum;
        counter++;


    }

1 个答案:

答案 0 :(得分:0)

我有理由认为我会在几个小时内看一下这个问题,然后在最后放弃并发布它的一两分钟内回答我自己的问题

try (BufferedReader br = new BufferedReader(new FileReader("\\Edi810o311214.dat")))
{
    int counter = 0;
    double total = 0;
    String line;
    while ((line = br.readLine())!= null)

    { 
    //eliminates lines that do not begin with "total"
    String lineEval = line.substring(0,5);
    if (lineEval.equals("Total"))

    {   
        counter++;
        //extracts total invoice field from line
        String totalValue = line.substring(12,26);
        double totalValueNum = Double.parseDouble(totalValue);
        total += totalValueNum;
        //shows output for testing purposes (will remove)
        System.out.println(totalValueNum);    






    }

    }
    System.out.println(counter);
    System.out.println(total);

    }

}