如何在Java中循环遍历CSV文件的一列以查找特定字符串?

3
我希望能够循环遍历csv文件的第二列,并找到特定字符串的索引。例如,查找所有值为“Chelsea”的索引。以下是我目前拥有的代码。
需要帮助吗?
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CSVReader {

    public static void main(String[] args) {
        CSVReader obj = new CSVReader();
        obj.run();
    }

    public void run() {
        String csv = "2015:2016.csv";
        BufferedReader br = null;
        String line = "";
        String csvSplit = ",";
        String[] football = new String[0];
        try {
            br = new BufferedReader(new FileReader(csv));
            String headerLine = br.readLine();

            while ((line = br.readLine()) != null) {
                football = line.split(csvSplit);
            }
        }
        catch (IOException io) {
            System.out.println(io);
        }
    }
}

1
football[1] 将会给你第二列。此外,你可能需要一个循环或者更好的方法(比如使用 Files.readAllLines() 一次性读取所有行)。 - TheLostMind
1
Files.readAllLines()会将您的整个文件加载到内存中,因此在使用此方法之前,请确保文件永远不会太大。 - Nicolas Filotto
2个回答

2

在寻找切尔西的过程中,你至少需要一个if语句。将以下代码合并到你的while循环中:

while ((line = br.readLine()) != null) {
    football = line.split(csvSplit);
    if(football[1].equals("Chelsea") {
        System.out.println("I found Chelsea!");
    }
}

当然,我想你希望比那更具有信息性。如果您想为Chelsea所在的行提供行号,可以这样做:
int index = 1;
while ((line = br.readLine()) != null) {
    football = line.split(csvSplit);
    if(football[1].equals("Chelsea") {
        System.out.println("I found Chelsea on line " + index);
    }
    index++;
}

如果你想要操作Chelsea的一行或多行元素,你可以这样做:

while ((line = br.readLine()) != null) {
    football = line.split(csvSplit);
    if(football[1].equals("Chelsea") {
        System.out.println("I found Chelsea on row with first column equal to " + football[0]);
        System.out.println("The entire row consists of: ");
        for(int i = 0; i < football.length; i++) {
            System.out.print(football[i] + ", ");
        }
        System.out.println();
    }
}

或者您可以将Chelsea的行输入到程序的其他部分中,以便进行其他高级操作。

太好了,正是我需要的!谢谢! - matebannan

1

这里有一个解决方案,与你的解决方案略有不同,但能实现你的需求。

public class Main {

public static void main(String[] args) throws FileNotFoundException, IOException {

    BufferedReader bfr = new BufferedReader(new FileReader("path_2_csv_file"));

    List<Integer> results = new ArrayList<>();
    String  l= "";
    int index = 1;
    while((l = bfr.readLine()) != null){

        //break each line of the csv file to its elements
        String[] elements=l.split(",");

        //check if the second column is equal to e.g. Chelsea
        if(elements[1].equals("Chelsea")){
            results.add(index);
        }

        index++;
    }

    //print the results
    for(Integer i : results){
        System.out.println(i);
    }
  }
}

嗨Meghi,好的非常感谢。一旦我找到索引,有没有办法使用这些索引在下一列中找到相应的值? - matebannan
是的。创建一个List<String[]> allLines = new ArrayList<String[]>()。在while循环的每次迭代中,将行存储在此列表中。allLines.add(l.split(",")) - MAZDAK
然后,您可以通过 allLines.get(i-1) 访问所需的索引(行)。 - MAZDAK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接