Java 3个csv文件,对每个文件求和,然后对所有文件求和

Java 3个csv文件,对每个文件求和,然后对所有文件求和,java,Java,我的任务是制作一个程序,从3个csv文件计算总和,首先计算每个文件的总和,然后在最后计算所有文件的总和并打印出来。我已经做了一个解决方案,但当运行程序时,它不会打印任何内容。检查工作目录及其良好状态 public class MoneyCounter { public void count(Path path) throws Exception { Files.walkFileTree(path, new SimpleFileVisitor<P

我的任务是制作一个程序,从3个csv文件计算总和,首先计算每个文件的总和,然后在最后计算所有文件的总和并打印出来。我已经做了一个解决方案,但当运行程序时,它不会打印任何内容。检查工作目录及其良好状态

public class MoneyCounter  {

        public void count(Path path) throws Exception {

            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

                    int[] amount = {0, 0, 0};

                    if (file.endsWith("csv")) {

                        BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(file)));

                        while(reader.ready()){
                            String line = reader.readLine();
                            if(line.split(" ")[1].equals("EUR")) amount[0] += Integer.parseInt(line.split(" ")[2]);
                            else if(line.split(" ")[1].equals("HRK")) amount[1] += Integer.parseInt(line.split(" ")[2]);
                            else amount[2] += Integer.parseInt(line.split(" ")[2]);
                        }

                        System.out.println("\"" + file + "\"" + " found");
                        System.out.println("\t Totals by currencies: ");

                        System.out.println("Money in all countries: ");
                        if(amount[0] > 0 ) System.out.println("\t EUR: " + amount[0]);
                        if(amount[1] > 0 ) System.out.println("\t HRK: " + amount[1]);
                        if(amount[2] > 0 ) System.out.println("\t USD: " + amount[2]);
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }

    public static void main(String[] args) throws Exception {
        MoneyCounter moneyCounter = new MoneyCounter();
        moneyCounter.count(new File("path.csv").toPath());
    }
}
公共类货币计数器{
公共无效计数(路径)引发异常{
walkFileTree(路径,新的SimpleFileVisitor(){
@凌驾
公共文件VisitResult visitFile(路径文件,基本文件属性属性属性)引发IOException{
int[]数量={0,0,0};
如果(文件.endsWith(“csv”)){
BufferedReader=new BufferedReader(new InputStreamReader(Files.newInputStream(file));
while(reader.ready()){
字符串行=reader.readLine();
如果(line.split(“”[1])等于(“欧元”)金额[0]+=Integer.parseInt(line.split(“”[2]);
否则如果(line.split(“”[1])等于(“HRK”))金额[1]+=Integer.parseInt(line.split(“”[2]);
else amount[2]+=Integer.parseInt(line.split(“”[2]);
}
System.out.println(“\”“+文件+”\”“+”找到”);
System.out.println(“\t按货币划分的总计:”);
System.out.println(“所有国家的货币:”);
如果(金额[0]>0)系统输出打印项次(“\t欧元:”+金额[0]);
如果(金额[1]>0)System.out.println(“\t HRK:+amount[1]);
如果(金额[2]>0)系统输出打印项次(“\t USD:+金额[2]);
}
返回FileVisitResult.CONTINUE;
}
});
}
公共静态void main(字符串[]args)引发异常{
MoneyCounter MoneyCounter=新的MoneyCounter();
moneyCounter.count(新文件(“path.csv”).toPath();
}
}
不会按照您的想法执行,它会检查路径的最后一个“元素”是否等于给定字符串

例如,路径“foo/bar”以“foo/bar”和“bar”结尾

它不以“r”或“/bar”结尾


尝试调用
file.toString().endsWith(“csv”)

它不满足endWith条件。 检查下面的工作代码

package files;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class Test {
    public static void main(String[] args) throws IOException {
        Test.count(new File("d:\\Files").toPath());
    }

    private static void count(Path path) throws IOException {
        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

                int[] amount = { 0, 0, 0 };
                BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(file)));

                while (reader.ready()) {
                    String line = reader.readLine();

                    if (line.split(" ")[1].equals("EUR"))
                        amount[0] += Integer.parseInt(line.split(" ")[2]);
                    else if (line.split(" ")[1].equals("HRK"))
                        amount[1] += Integer.parseInt(line.split(" ")[2]);
                    else
                        amount[2] += Integer.parseInt(line.split(" ")[2]);
                }

                System.out.println("\"" + file + "\"" + " found");
                System.out.println("\t Totals by currencies: ");

                System.out.println("Money in all countries: ");
                if (amount[0] > 0)
                    System.out.println("\t EUR: " + amount[0]);
                if (amount[1] > 0)
                    System.out.println("\t HRK: " + amount[1]);
                if (amount[2] > 0)
                    System.out.println("\t USD: " + amount[2]);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
按货币分列的总数:

所有国家的货币:

 EUR: 10
 HRK: 20
 HRK: 20
 USD: 10
按货币分列的总数:

所有国家的货币:

 EUR: 10
 HRK: 20
 HRK: 20
 USD: 10

数据如何存储在csv中?尝试了您的解决方案,但在线程“main”java.lang.ArrayIndexOutOfBoundsException中出现此错误异常:索引1超出长度1的界限,位于com.dino.MoneyCounter$1.visitFile(MoneyCounter.java:27),位于com.dino.MoneyCounter$1.visitFile(MoneyCounter.java:15),位于java.base/java.nio.file.walkFileTree(Files.java:2724)在java.base/java.nio.file.Files.walkFileTree(Files.java:2796)在com.dino.MoneyCounter.count(MoneyCounter.java:15)在com.dino.MoneyCounter.main(MoneyCounter.java:47)在线程“main”java.lang.NumberFormatException中异常:对于输入字符串:“application”在java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)在java.base/java.lang.Integer.parseInt(Integer.java:652)在java.base/java.lang.Integer.parseInt(Integer.java:770)在com.dino.Main$1.visitFile(Main.java:37)在com.dino.Main$1.visitFile(Main.java:22)在java.base/java.nio.file.Files.walkFileTree(Files.java:2724)在java.base/java.nio.Files.Files.walkFileTreecom.dino.Main.count(Main.java:22)和com.dino.Main.Main(Main.java:18)上的(Files.java:2796)试着用3个单独的文件运行代码,每个文件都有3种不同的货币。你会得到什么?我知道我可能很无聊,但你帮了很多忙。我还有一个问题要问。你用3个文件完成了,其中只有一行。如果你有这样的文件,你会得到什么:1-2-3-