0

I have an array of call records and I want to store the ones that have a call disposition of "DNC" in a different array. The function opens a csv file and reads the entire file into an array of objects and sets the attributes. The original array contains 680 object records. I have an if statement after the my object setters that says if userArray[count].getDisposition == "DNC" then add this records to my new array DNC_List[dnc_count] and increment dnc_count++.

dnc_count is initialized to 0 and at the end of the process, it is still 0. There are 4 records in my original array that have "DNC" as an attribute. Can anyone tell me what is wrong with my code and why it is not recognizing when disposition is DNC?

public static void myCSVReader(String filename) {
    String path = "C:\\Users\\My_Username\\Downloads\\" + filename;
    file_path = path;
    BufferedReader reader = null;
    String line = "";
    String csvSplitBy = ",";
    int count = 0;
    String fullName, U_ID;
    int dnc_count = 0;

    User userArray[] = new User[1000];
    User[] DNC_List = new User[1000];
    System.out.println("Starting csv reader");

    try {
        reader = new BufferedReader(new FileReader(path));
        while ((line = reader.readLine()) != null) {
            String[] s = line.split(csvSplitBy);

            fullName = s[13] + " " + s[15];
            U_ID = s[44];
            userArray[count] = new User(s[1], s[2], s[0], fullName, U_ID);
            userArray[count].setDisposition(s[2]);
            userArray[count].setNumber(s[1]);
            userArray[count].setName(fullName);
            userArray[count].setID(U_ID);
            if(userArray[count].getDisposition() == "DNC" || userArray[count].getDisposition() == "CS"){
                DNC_List[dnc_count].setKey(String.valueOf(dnc_count));
                DNC_List[dnc_count].setDisposition("DNC");
                DNC_List[dnc_count].setName(userArray[dnc_count].getName());
                DNC_List[dnc_count].setID(userArray[dnc_count].getID());
                dnc_count++;
            }
            count++;
            User user = new User(s[1], s[2], s[0], fullName, U_ID);
            user.setDisposition(s[2]);
            user.setNumber(s[1]);
            user.setName(fullName);
            user.setID(U_ID);

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Prints out the array index, name, disposition, and phone_number
    System.out.println("\n\nUser array: \n");
    for (int i = 0; i < userArray.length; i++) {
        if (userArray[i] != null) {
            System.out.println(i + ": " + userArray[i].getName() + "\t | Dispo = " + userArray[i].getDisposition()
                    + "\t | Phone = " + userArray[i].getNumber());
        }
    }
    
    System.out.println("\nDNC_COUNT = " + dnc_count);
    for (int i = 0; i < dnc_count; i++) {
        System.out.println(DNC_List[i] + "\n");
    }
}
AnthonyH
  • 47
  • 7

2 Answers2

1

The value of the string object can't be compared using '==' operator and should use the equals() method.

Change your comparison to use equals method of String class.

Use

userArray[count].getDisposition().equals("DNC") "CS"

instead of

userArray[count].getDisposition() == "DNC" 
ashishbg
  • 25
  • 1
  • 5
Prabhu
  • 129
  • 1
  • 1
  • 9
  • Awesome thank you so much. This solved my issue. – AnthonyH Dec 04 '20 at 15:02
  • You are Welcome! and your upvote matters! – Prabhu Dec 04 '20 at 15:12
  • @Prabhu, such issue is a low-hanging fruit which is generally worth a comment. And the question is very likely to be closed soon as a duplicate and then removed. – Nowhere Man Dec 04 '20 at 15:19
  • @AlexRudenko Does this effect my profile on stackoverflow if the question is closed for being a duplicate? – AnthonyH Dec 04 '20 at 16:37
  • @AnthonyHorner, if the question is closed, it does not affect your reputation points, it just won't allow new answers. Only when a question is _removed_, all the related points (positive or negative) are discarded, however, the chances for "duplicate" questions to be removed are pretty high. – Nowhere Man Dec 04 '20 at 17:23
0

Try with equals() method instead of ==

public class Main {
  public static void main(String[] args) {
    String a = "DNC";
    String b = new String("DNC");
    System.out.println(a == b);
    System.out.println(a.equals(b));
  }
}

Output:

false
true
Tushar
  • 670
  • 3
  • 14