java 为什么变量不递增

tzdcorbm  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(117)

我正在尝试使用java分析csv文件中的数据。根据数据,变量numberPositive应该递增,但它没有。我检查了e.equals方法中的String,它与数据文件中的字符串完全匹配。有人知道错误在哪里吗?谢谢

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class project {
    
    public static void main(String[] args) {
        String path= "C:\\Users\\USER\\Dropbox\\My PC\\Desktop\\data.csv";
        String line = "";
        String cvsSplitBy = ",";
        int variableColumn = 2;
        int numberPositive = 0;
        var numberExperiments= 10.0;
       // int targetIndex = 0;
        int targetColumn = 0;
        int startingRow =5;
        int currentRow =0; 
       
        try (Scanner write = new Scanner (System.in)) {
            ArrayList<String> Variable = new ArrayList<>();
            ArrayList<Double> Distance = new ArrayList <>();
            System.out.println("Enter the name of the fish");
            
            String nameOfFish = write.next();
            HashMap<String,Integer> fishCodes = fishCodes();
            if (fishCodes.containsKey(nameOfFish)) {
                targetColumn = getValue(fishCodes,nameOfFish);
            }
            
            try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

                // Read the CSV file line by line
                while ((line = br.readLine()) != null) {
                    currentRow++;
                    // Split the line into an array of cells
                    String[] cells = line.split(cvsSplitBy);
                    // Add the data in the specific column to the ArrayList
                    if (currentRow>=startingRow&&currentRow<1215) {
                    Variable.add(cells[variableColumn-1]);
                  Distance.add(Double.parseDouble(cells[targetColumn-1]));
                }}

            } catch (IOException e) {
                e.printStackTrace();
            }
     for (String e: Variable) {
              if (e.equalsIgnoreCase("VIB1 DISTANCE")) {
                 
                  double priorDistance = Distance.get(Variable.indexOf(e)-1);
                 double VibDistance = Distance.get(Variable.indexOf(e));
                  
                  if ((VibDistance - priorDistance) >= priorDistance){
                      numberPositive++;
                  }}
     }
     System.out.println(numberPositive);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(PercentageofPositive(numberPositive, numberExperiments));
      
 
    }
    private static int getValue(HashMap<String, Integer> fishCodes, String nameOfFish) {
        // TODO Auto-generated method stub
        return fishCodes.get(nameOfFish);
    }
    public static double PercentageofPositive (int a,double b) {
        return a/b*100;
    }
    public static HashMap <String,Integer> fishCodes(){
        HashMap<String, Integer> map = new HashMap<>();
        map.put("A1",4);
        map.put("A2",5);
        map.put("A3",6);
        map.put("A4",7);
        map.put("A5",8);
        map.put("A6",9);
        map.put("B1",10);
        map.put("B2",11);
        map.put("B3",12);
        map.put("B4",13);
        map.put("B5",14);
        map.put("B6",15);
        map.put("C1",16);
        map.put("C2",17);
        map.put("C3",18);
        map.put("C4",19);
        map.put("C5",20);
        map.put("C6",21);
        map.put("D1",22);
        map.put("D2",23);
        map.put("D3",24);
        map.put("D4",25);
        map.put("D5",26);
        map.put("D6",27);
        return map;
        
        
    }}

我好像漏掉了什么,但我想不出来

iq0todco

iq0todco1#

您不能使用增强的for循环(for (String e: Variable))。
从循环的内容(Distance.get(Variable.indexOf(e)))看起来您想要访问与Variable列表相同索引处的Distance列表,但是使用增强的for循环和Variable.indexOf(e)不允许这样做-Variable.indexOf(e)总是返回找到e第一个索引(“VIB 1 DISTANCE”)(它甚至可能无法找到e,因为它可能是“vib 1 distance”,而indexOf()会进行区分大小写的比较)。
解决方案是使用经典的索引for循环:

for (int i = 0; i < Variable.size(); i++) {
         if (Variable.get(i).equalsIgnoreCase("VIB1 DISTANCE")) {
             
             double priorDistance = Distance.get(i-1);
             double VibDistance = Distance.get(i);
              
             if ((VibDistance - priorDistance) >= priorDistance) {
                 numberPositive++;
             }
         }
     }

这可能仍然有问题(如果第一行包含“VIB 1 DISTANCE”怎么办?),但将解决第一个问题。

相关问题