Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.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 检测CSV文件中出现的行数_Java_Parsing_Csv_File Io_Conditional Statements - Fatal编程技术网

Java 检测CSV文件中出现的行数

Java 检测CSV文件中出现的行数,java,parsing,csv,file-io,conditional-statements,Java,Parsing,Csv,File Io,Conditional Statements,在出现特定情况时,如何从使用OpenCSV库解析的文件中获取行号?我想用CSV文件中的行号警告用户,只要在该行上检测到潜在的错误值 下面是我将文件解析为字符串数组的一段代码: try { CSVReader reader = new CSVReader(new FileReader(filePath), ','); // Reads the complete file into list of tokens. List<String[]> rowsAsTo

在出现特定情况时,如何从使用OpenCSV库解析的文件中获取行号?我想用CSV文件中的行号警告用户,只要在该行上检测到潜在的错误值

下面是我将文件解析为字符串数组的一段代码:

try 
{
    CSVReader reader = new CSVReader(new FileReader(filePath), ',');

    // Reads the complete file into list of tokens.
    List<String[]> rowsAsTokens = null;

    try 
    {
        rowsAsTokens = reader.readAll();
    } 

    for(String[] row: rowsAsTokens) 
    {
        //Check for conditions within CSV file
    }

    catch (IOException e1)
    {
        e1.printStackTrace();
    }
}
试试看
{
CSVReader reader=newcsvreader(newfilereader(filePath),',');
//将完整文件读入令牌列表。
List rowsAsTokens=null;
尝试
{
rowsAsTokens=reader.readAll();
} 
对于(字符串[]行:rowsAsTokens)
{
//检查CSV文件中的条件
}
捕获(IOE1异常)
{
e1.printStackTrace();
}
}
关于:

   int line = 0;
   for(String[] row: rowsAsTokens) 
   {

        //Check for conditions within CSV file
        if (isErro) {
            String errMsg = "error in line: " + line;
            // output errrMsg;
        }
        line++;
   }

为循环设置计数器,以便在解析CSV时计算行号

try 
{
    CSVReader reader = new CSVReader(new FileReader(filePath), ',');

    // Reads the complete file into list of tokens.
    List<String[]> rowsAsTokens = null;

    int lineNum = 0;

    try 
    {
        rowsAsTokens = reader.readAll();
    } 

    for(String[] row: rowsAsTokens) 
    {
        lineNum++; // increment the line number
        //Check for conditions within CSV file
        if (ERRONEOUS VALUE) {
            // save the lineNum. possibly to an array or a string. whatever you need
        }
    }

    catch (IOException e1)
    {
        e1.printStackTrace();
    }
}
试试看
{
CSVReader reader=newcsvreader(newfilereader(filePath),',');
//将完整文件读入令牌列表。
List rowsAsTokens=null;
int lineNum=0;
尝试
{
rowsAsTokens=reader.readAll();
} 
对于(字符串[]行:rowsAsTokens)
{
lineNum++;//增加行号
//检查CSV文件中的条件
if(错误值){
//保存lineNum。可能是数组或字符串。需要什么就保存什么
}
}
捕获(IOE1异常)
{
e1.printStackTrace();
}
}

您知道如何将整数行计数器提高1吗?