java - 如何在一种方法中获取从 java 中的 CSV 文件读取的值并在另一种方法中使用它们?

标签 java csv parsing methods field

我读取了包含 2x2 矩阵因子的 CSV 文件,现在我尝试将这些值发送到用于操作卡方检验的方法。我尝试将值返回到名为 statResults 的类,然后在调用 statResults 的不同类中使用它们。

public class CSVReaderJava {

    public double getComponents() throws FileNotFoundException {

        File csv = new File("Matrices.csv");    
        Scanner scnr = new Scanner(csv);    
        scnr.useDelimiter(Pattern.compile(",|$", Pattern.MULTILINE));

        int lineNumber = 1;

        scnr.nextLine();
        scnr.nextLine();
        while(scnr.hasNextLine() && lineNumber == 10){

            String targRef = scnr.next();
            double targref = Double.parseDouble(targRef);
            String targett = scnr.next();
            double target2 = Double.parseDouble(targett);
            String baseRef = scnr.next();
            double baseref = Double.parseDouble(baseRef);
            String baseTarg = scnr.next();
            double basetarget = Double.parseDouble(baseTarg);
            String fish2 = scnr.next();           
            String fishLeft = scnr.next();
            String fishRight = scnr.next();          
            String expValue = scnr.next();

            System.out.println(lineNumber + " :" +targref+" "+target2+" "+baseref+" "+
                    basetarget+" "+fish2+" "+fishLeft+" "+fishRight+" "+expValue);
            System.out.println("--------------------------");
            scnr.nextLine();
            scnr.nextLine();
            lineNumber++;   

            statResults components = new statResults();
            components.targetReference = targref;
            components.baseReference = baseref;
            components.target = target2;
            components.baseTarget = basetarget;        
            return components;
        }         
        scnr.close();      

    }   
    public statResults getStatResults() {
        return getComponents();
    }

我收到以下错误:返回组件“类型不匹配:无法从 statResults 转换为 double”,以及 elements.targetReference/others“targetReference 无法解析或不是字段”

我在这里声明了字段:

public class statResults {
        public double rightTail;
        public double leftTail;
        public double twoTail;

        public double targetReference;
        public double target;
        public double baseReference;
        public double baseTarget;                   
}

不知道下一步该怎么做,帮帮我

最佳答案

您的 getComponents 方法应返回 statResults,而不是 double 值。

按如下方式更改方法签名:

public statResults getComponents() throws FileNotFoundException {

      // your code...

}

这应该可以解决您的问题。

关于java - 如何在一种方法中获取从 java 中的 CSV 文件读取的值并在另一种方法中使用它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57647780/

相关文章:

ruby - JSON的基于流的解析和写入

c++ - 如何将更改的行与 C 代码的 git 存储库中的函数相关联?

java - Firebase 值事件监听器即使在 Activity 完成后也会触发?

java - 使用 Java 从数据库 View 中未检索到结果

c# - 解析巨大的 CSV 并加载到 MySQL DB

csv - 将 CSV 文件导入 Hadoop

java - 如何@Override Comparator使CellTable中的列基于整数而不是基于字符串(GWT)排序?

Java/JUnit - AssertTrue 与 AssertFalse

batch-file - 合并不带标题的 CSV 文件

parsing - Gold Parsing System - 它在编程中有什么用?