Reputation: 101
My program is to validate if the input code number is empty or not. The condition is if there is a code number entered(thru csv file) proceed and if code number is empty an error message show. "The code number is empty in line :__". My problem is how can I suppose to print the index of a line on which the code number was empty.
Here's my sample data(csv):
CRITERIA CODE NAME AGE ADDRESS
ADD 0001 JOHN 21 USA
ADD MICH 16 EUR
ADD ALI 11 PHL
Error Message should be :
"The code number is empty in line 2."
"The code number is empty in line 3."
Here's my current program :
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;
}
I hope someone will help me on this. Thank you!
Upvotes: 1
Views: 3833
Reputation: 6435
you can add a counter, which is incremented every .readNext()
and let it print if there is an error
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;
}
Upvotes: 1