user2703771
user2703771

Reputation: 51

Issue with .equals() function in Java

I'm having an issue with the .equals function in Java. I'm pretty sure the issue is with the two strings not being the same even though they look it to the physical eye (different formats). But I don't seem to be able to debug it so was wondering could you guys maybe help me out?

Basically this is just a short piece of code that will be used for reading a .CSV and parse back the first 1 - 3 words minus any numbers. I know it could be coded better but it's something that will only be ran once. It works fine but it is suppose to remove duplicates, which currently isn't working. If you guys could shine some light on how I can fix this it be greatly appreciated.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadCSVTest {
    public static void main(String[] args) { 
        ReadCSVTest obj = new ReadCSVTest();
        obj.run(); 
    }

    public void run() {
        String csvFile = "C:/Users/mrx/Documents/Standard Software.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";
        int count = 0;
        String duplicate = "";

        try {
            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) { 

                String[] product = line.split(cvsSplitBy);
                String app = product[0];
                app = app.toUpperCase();
                app = app.replaceAll("[^A-Z -]", "");
                app = app.trim();
                if(!app.equals(duplicate)) {
                    if(app.contains(" ")){
                        String words[] = app.split(" ");
                        String firstTwo = words[0] + " " + words[1]; 
                        if(firstTwo.contains("MICROSOFT")){
                            if(words.length > 2){
                                String firstThree = words[0] + " " + words[1] + " " + words[2];
                                duplicate = firstThree.trim();
                                System.out.println("\"" + duplicate + "\", ");
                                count++;
                            }
                            else{
                                duplicate = firstTwo.trim();
                                System.out.println("\"" + duplicate + "\", ");
                                count++;
                            }
                        }
                        else{
                            duplicate = firstTwo.trim();
                            System.out.println("\"" + duplicate + "\", ");
                            count++;
                        }
                    }
                    else{
                        duplicate = app.trim();
                        System.out.println("\"" + duplicate + "\", ");
                        count++;
                    }
                }
            }
        } 
        catch (FileNotFoundException e){ e.printStackTrace();} 
        catch (IOException e){ e.printStackTrace();} 
        finally{
            if (br != null) {
                try{ br.close(); } 
                catch (IOException e) { e.printStackTrace();}
            }
        } 
        System.out.println("Done");
        System.out.println(count + " Programs");
    }
}

Upvotes: 0

Views: 187

Answers (1)

JB Nizet
JB Nizet

Reputation: 692191

Here's how to debug such an issue. Call the following method with the two strings that look equal but are not:

public void dumpString(String s) {
    System.out.println("Length: " + s.length()); // check their length
    System.out.println("Value: >" + s + "<"); // check trailing and leading spaces
    System.out.println("numeric value of characters:"); // check which char differs
    for (int i = 0; i < s.length(); i++) {
        System.out.print((int) s.charAt(i));
        System.out.print(' ');
    }
}

Using a debugger lets you access the same information.

Upvotes: 5

Related Questions