0

您将如何从使用 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();
    }
}
4

2 回答 2

2

为您的循环设置一个计数器,以在解析 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();
    }
}
于 2013-02-11T20:22:53.850 回答
0

关于什么:

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

        //Check for conditions within CSV file
        if (isErro) {
            String errMsg = "error in line: " + line;
            // output errrMsg;
        }
        line++;
   }
于 2013-02-11T20:21:43.457 回答