如何在java中获取csv文件的行索引

如何在java中获取csv文件的行索引,java,arrays,csv,Java,Arrays,Csv,我的程序是验证输入代码是否为空。条件是如果输入了代码(通过csv文件),则继续,如果代码为空,则显示错误消息。“第:\行中的代码为空。”。 我的问题是如何打印代码为空的行的索引 以下是我的示例数据(csv): 错误消息应为: "The code number is empty in line 2." "The code number is empty in line 3." 以下是我目前的计划: private static String[] sNextLine2; public stati

我的程序是验证输入代码是否为空。条件是如果输入了代码(通过csv文件),则继续,如果代码为空,则显示错误消息。“第:\行中的代码为空。”。 我的问题是如何打印代码为空的行的索引

以下是我的示例数据(csv):

错误消息应为:

"The code number is empty in line 2."
"The code number is empty in line 3."
以下是我目前的计划:

private static String[] sNextLine2; 
public static Map<String,Employee> getChanges
( String sFileName, Map<String, Employee> mEmployeeList )
throws IOException {
                                    //Read_File
    setReader2(new CSVReader(new FileReader(sFileName)));
    while ((sNextLine2 = reader2.readNext()) != null) { 
    switch(sNextLine2[0]) {
        case "ADD":         
            if(sNextLine2[1].isEmpty()) {
                System.out.println("The code number is empty in line" + lineNumber); //how to get that line number

            } else if (mEmployeeList.containsKey(sNextLine2[1]))
            {
                System.out.println("Data already exist");
            }
            else
            {
                mEmployeeList.put(sNextLine2[1],new Employee(sNextLine2[1],
                        sNextLine2[2], sNextLine2[3], sNextLine2[4], sNextLine2[5],
                        sNextLine2[6], sNextLine2[7], sNextLine2[8]));
            }

            break;
    } 
私有静态字符串[]sNextLine2;
公共静态映射getChanges
(字符串sFileName,映射mEmployeeList)
抛出IOException{
//读取文件
setReader2(新的CSVReader(新的文件阅读器(sFileName));
而((sNextLine2=reader2.readNext())!=null){
开关(sNextLine2[0]){
案例“添加”:
if(sNextLine2[1].isEmpty()){
System.out.println(“第行代码为空”+lineNumber);//如何获取该行号
}else if(mEmployeeList.containsKey(sNextLine2[1]))
{
System.out.println(“数据已经存在”);
}
其他的
{
mEmployeeList.put(sNextLine2[1],新员工(sNextLine2[1],
sNextLine2[2],sNextLine2[3],sNextLine2[4],sNextLine2[5],
sNextLine2[6],sNextLine2[7],sNextLine2[8]);
}
打破
} 

我希望有人能在这方面帮助我。谢谢!

您可以添加一个计数器,它每
一次递增一次。readNext()
并在出现错误时让它打印出来

 int counter=0;
 while ((sNextLine2 = reader2.readNext()) != null) { 
    counter++;
    switch(sNextLine2[0]) {
        case "ADD":         
            if(sNextLine2[1].isEmpty()) {
                System.out.println("The code number is empty in line" + counter); //how to get that line number

            } else if (mEmployeeList.containsKey(sNextLine2[1]))
            {
                System.out.println("Data already exist");
            }
            else
            {
                mEmployeeList.put(sNextLine2[1],new Employee(sNextLine2[1],
                        sNextLine2[2], sNextLine2[3], sNextLine2[4], sNextLine2[5],
                        sNextLine2[6], sNextLine2[7], sNextLine2[8]));
            }

            break;
    } 

非常感谢你,你太棒了!你一分钟就搞定了。
 int counter=0;
 while ((sNextLine2 = reader2.readNext()) != null) { 
    counter++;
    switch(sNextLine2[0]) {
        case "ADD":         
            if(sNextLine2[1].isEmpty()) {
                System.out.println("The code number is empty in line" + counter); //how to get that line number

            } else if (mEmployeeList.containsKey(sNextLine2[1]))
            {
                System.out.println("Data already exist");
            }
            else
            {
                mEmployeeList.put(sNextLine2[1],new Employee(sNextLine2[1],
                        sNextLine2[2], sNextLine2[3], sNextLine2[4], sNextLine2[5],
                        sNextLine2[6], sNextLine2[7], sNextLine2[8]));
            }

            break;
    }