gpt4 book ai didi

java - 读取缓冲区中的csv,检查是否通过检查 -> 将信息逐行转发到方法

转载 作者:行者123 更新时间:2023-12-01 21:52:02 24 4
gpt4 key购买 nike

在我的程序中,我可以读取 csv 文件。我逐行将信息解析为方法。但是如果 csv 没有通过 if 检查,我不想调用“otherMethod(id, new);”根本不。因此,在调用 otherMethod 之前,必须传递整个 CSV。

所以我的问题是:如何在某种缓冲区中读取整个 csv,检查它是否通过 if 检查并随后处理它(逐行)?

boolean csvPassed = true;
int index = 0;
double id = Double.NaN, new = Double.NaN;

while ((String line = reader.readLine()) != null) {
Scanner scanner = new Scanner(line);
scanner.useDelimiter(";");
while (scanner.hasNext()) {
//check the csv line by line
if (index == 0)
id = Integer.parseInt(data);
else if (index == 1) {
new = Integer.parseInt(data);
if (new == 10)
csvPassed = false
}
//whatever you want to do here
}
}

//after the file is checked
if(csvPassed){
// If the csv passes the check, call the method line by line...
otherMethod(id, new);
}

最佳答案

这可能对你有帮助。

BufferedReader reader = new BufferedReader(new FileReader("<YOUR_FILENAME>"));
//your buffer to store the lines of your csv
List<Integer[]> lines = new LinkedList<>();
//a flag to indicate the success
boolean passed = true;
//read in the file line by line
String currentLine;
while((currentLine = reader.readLine()) != null){
//split the line up at your delimiter
String[] lineSplit = currentLine.split(";");
//the array to store the parsed integers of the current line
Integer[] parsedLine = new Integer[lineSplit.length];

//process each element of that line and check for your condition
for(int i = 0; i < lineSplit.length; i++){
//make sure you can parse that string
parsedLine[i] = Integer.parseInt(lineSplit[i]);
if(parsedLine[i] == 10){
//Maybe you could also exit your loop here and clear the list?
passed = false;
}
}
//add this line to your buffer
lines.add(parsedLine);
}

//If all lines passed the condition, process line by line out of your buffer
if(passed){
//make sure you have the 2 values in the array
for(Integer[] line : lines){
otherMethod(line[0], line[1]);
}
}

关于java - 读取缓冲区中的csv,检查是否通过检查 -> 将信息逐行转发到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35065353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com