简体   繁体   中英

How do I take values read from a CSV file in java in one method and use them in another method?

I read in a CSV file with factors for 2x2 matrices, and now I am trying to send these values to a method where they're used to operate a chi-square test. Im trying to return the values to a class called statResults, and then use them in a different class that calls 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();
    }

I get errors for return components "Type mismatch: cannot convert from statResults to double" and for components.targetReference/others "targetReference cannot be resolved or is not a field"

I declared the fields here:

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

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

Idk what to do next help me

Your getComponents method should return statResults, and not a double.

Change your method signature as follows:

public statResults getComponents() throws FileNotFoundException {

      // your code...

}

This should solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM