mardi 4 août 2020

hashMap both if and else condition executed

I am unable to figure out why my block of code is executing both the If and Else condition at the same instant.

This is my code

private HashMap<String, Integer> recordCounterMap = new HashMap<String, Integer>();

if(lastName.isNull())
{
     if (!recordCounterMap.containsKey(custName)) {    // This is executed
        System.out.println("Null occurred 1st time for " + custName);
        recordCounterMap.put(custName, ctr);
        ctr=0;              //reset the counter variable
         }
     else {                                          // As well as This is executed at the same instant
        if (recordCounterMap.containsKey(custName)) {
        System.out.println("Counter value for " + custName + " is "+recordCounterMap.get(custName ));
            if (recordCounterMap.get(instrument) > 1) {
            System.err.println("For 3 times the value is Null, for " + custName);
            }
            else{
            System.out.println("Counter value for " + custName + " is Null a 2nd time ");
            ctr=recordCounterMap.get(instrument) + 1; // Adding the value from the HashMap
            recordCounterMap.put(custName, ctr);
            ctr=0;              //reset the counter variable
            }
        }
      }
}

File contains 1 million rows Below is a subset of data

Customer Name, First Name, Last Name
Rob Jones, Rob, Jones
Tom Crawford,Tom, Crawford
Lana Hainsely, Lana,                 <--Last Name is null but only 1 record where Last Name is null
Milford Dsouza, Milford, Dsouza

I read Customer Name into custName and Last Name into lastName

On running the code, I get the output as

Null occurred 1st time for Lana Hainsely
Counter value for Lana Hainsely is Null a 2nd time 
For 3 times the value is Null, for Lana Hainsely

Am not sure why both the if and else condition are executed.

Any advice / guidance appreciated. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire