如何在csv文件的列中循环查找java中的特定字符串?
我希望能够循环浏览csv文件的第二列,并找到特定字符串的索引。例如,找到所有值为“Chelsea”的索引。下面是我到目前为止的代码 有什么帮助吗如何在csv文件的列中循环查找java中的特定字符串?,java,csv,Java,Csv,我希望能够循环浏览csv文件的第二列,并找到特定字符串的索引。例如,找到所有值为“Chelsea”的索引。下面是我到目前为止的代码 有什么帮助吗 import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class CSVReader { public static void main(String[] args) { CSVReader o
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);
}
}
}
这是一个与您的解决方案稍有不同的解决方案,但它能满足您的需求
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);
}
}
}
公共类主{
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
BufferedReader bfr=新的BufferedReader(新文件读取器(“路径2\u csv\u文件”);
列表结果=新建ArrayList();
字符串l=“”;
int指数=1;
而((l=bfr.readLine())!=null){
//将csv文件的每一行打断为其元素
字符串[]元素=l.split(“,”);
//检查第二列是否等于,例如Chelsea
if(元素[1]。等于(“切尔西”)){
结果:添加(索引);
}
索引++;
}
//打印结果
for(整数i:结果){
系统输出打印LN(i);
}
}
}
就找到切尔西而言,你至少只需要一句if语句。将此代码合并到while循环中:
while ((line = br.readLine()) != null) {
football = line.split(csvSplit);
if(football[1].equals("Chelsea") {
System.out.println("I found 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++;
}
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();
}
}
或者,如果你想处理切尔西排中的一个或所有因素,你可以做如下事情:
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++;
}
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();
}
}
while((line=br.readLine())!=null){
足球=直线分割(csvSplit);
如果(足球[1]。等于(“切尔西”){
System.out.println(“我发现切尔西在同一排,第一列等于”+football[0]);
System.out.println(“整行包括:”);
对于(int i=0;i
或者你可以将Chelsea的台词输入到你程序的另一部分,用它做其他有趣的事情。
football[1]
将为你提供第二栏。此外,你可能需要一个循环或更好的方法(比如使用文件.readAllLines()
文件.readAllLines()
会将所有文件加载到内存中,因此在使用此方法之前,请确保文件永远不会变大这正是我所需要的!谢谢!嗨,Meghi,好的,非常感谢,一旦我找到索引,是否仍要使用这些索引在下一列中查找相应的值?是。创建一个List allLines=new ArrayList()
。在while循环的每次迭代中,将行存储在此列表中。allLines.add(l.split(“,”)
。然后您可以通过allLines.get(i-1)
访问所需的索引(行)。