Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在字符串数组中搜索字符串并在java中返回其计数?_Java - Fatal编程技术网

如何在字符串数组中搜索字符串并在java中返回其计数?

如何在字符串数组中搜索字符串并在java中返回其计数?,java,Java,我读取了一个csv文件,将所有数据放入字符串类型数组并显示该数组。现在我想搜索该数组中的字符串,最后返回在数组中找到该字符串的次数。我的代码在下面 package countscv; import com.opencsv.CSVReader; import java.io.FileNotFoundException; import java.io.IOException; import javax.swing.JOptionPane; import java.io.FileReader; i

我读取了一个csv文件,将所有数据放入字符串类型数组并显示该数组。现在我想搜索该数组中的字符串,最后返回在数组中找到该字符串的次数。我的代码在下面

package countscv;

import com.opencsv.CSVReader;
import java.io.FileNotFoundException;

import java.io.IOException;
import javax.swing.JOptionPane;
import java.io.FileReader;
import java.util.Arrays;

public class Countscv {
    /** @param args the command line arguments */
    public static void main(String[] args) throws FileNotFoundException, IOException {

        //Build reader instance
        //Read data.csv
        //Default seperator is comma
        //Default quote character is double quote
        //Start reading from line number 2 (line numbers start from zero)
        CSVReader reader = new CSVReader(new FileReader("res.csv"), ',' , '"' , 1);

        //Read CSV line by line and use the string array as you want
        String search="Brazil";
        int counter=0;
        String[] nextLine;
        while ((nextLine = reader.readNext()) != null) {
            if (nextLine != null) {
                //Verifying the read data here
                System.out.println(Arrays.toString(nextLine));
            }
        }
        for(int i=0;i<nextLine.length;i++)
        {
            if((nextLine[i].equals(search)))
            {
                counter++;
            }
        }
        System.out.print(counter);
    }
}
package countscv;
导入com.opencsv.CSVReader;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入javax.swing.JOptionPane;
导入java.io.FileReader;
导入java.util.array;
公共类Countscv{
/**@param指定命令行参数*/
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
//构建阅读器实例
//读取数据.csv
//默认分隔符是逗号
//默认的引号字符是双引号
//从行号2开始读取(行号从零开始)
CSVReader reader=新的CSVReader(新的文件读取器(“res.csv”),,,,,,“,”,1);
//逐行读取CSV,并根据需要使用字符串数组
字符串搜索=“巴西”;
int计数器=0;
字符串[]下一行;
而((nextLine=reader.readNext())!=null){
如果(下一行!=null){
//在此处验证读取的数据
System.out.println(Arrays.toString(nextLine));
}
}

for(int i=0;ifor循环正在尝试使用nextLine,因为while循环已到达其终点,因此nextLine在该点为null。您需要在while循环内计数。请尝试以下操作:

CSVReader reader = new CSVReader(new FileReader("res.csv"), ',' , '"' , 1);
String search = "Brazil";
int counter = 0;
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
    for(String word : nextLine) {
        if (word.equals(search)) {
            counter++;
        }
    }
}
System.out.println(counter);

如何解决该问题的可能重复?我确信我正在搜索的字符串将在该数组中多次找到,堆栈跟踪是什么?在我用于匹配字符串的for循环上,哪个行上有
NullPointerException
on?发布堆栈跟踪。顺便说一句,while循环中的if语句是redundant.it似乎工作正常。谢谢,但是我应该在哪里打印计数器的总计数?外部while循环?好的,谢谢,这工作正常,但我有一个问题。我的数据当前以字符串数组的形式显示(每行的每个字符串数组)。如何将csv文件中的所有数据保存到单个数组中,然后进行搜索?这将非常有帮助。