0

So I have a program that I'm working on to format and query google for lat and long coordinates based on locations I have in a CSV file. The program is by no means efficient and I honestly do not care as long as it formats an output formatted correctly that I can use for a XML file. Google has a limit on queries so I decided to store previous queries in a basic text file and then check using buffer reader to see if the line.contains(companyname) but it never matches. I print out both the string that is being compared to the contains and the line and they are identical in print out but lengths are different by 1. I'm assuming there are some hidden characters or something that is throwing of the contains() method.

Sample output:

enter image description here

Below is the two important methods to this process.

public static void writeCordFile(String company, String lat, String log) {
    try {
        System.out.println("writing to /Users/ASheridan/Downloads/cord" +"data:"+company + "lat: " + lat + "log" + log);
        FileWriter writer = new FileWriter("/Users/ASheridan/Downloads/cord", true);


            //writer.write(company + " lat: " + lat + " log: " + log);
            writer.write(company+"  ");
            writer.write("\r\n");
            writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }





}


public static boolean checkIfCordinatesExist(String companyName) {
    BufferedReader br = null;
    String cordStorage = "/Users/ASheridan/Downloads/cord";
    String lineCord = "";
    try {

        br = new BufferedReader(new FileReader(cordStorage));
        System.out.println("CHECKING THE FULL FILE FOR MATCHES");
        int i=0;
        while ((lineCord = br.readLine()) != null) {

            System.out.println(i);
            i++;
            System.out.println("compare to: "+lineCord.compareTo(companyName));
            System.out.println("line length: "+lineCord.length());
            System.out.println("company length: "+companyName.length());
            if (lineCord.toLowerCase().contains(companyName.toLowerCase())) {
                System.out.println("Existing Cordinates for " + cordStorage + " found");
                return true;
            }

            else {
                System.out.println("Result for " + companyName +"not found in line:" + lineCord);
            }



        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return false;
}
Andrew Sheridan
  • 19
  • 1
  • 10

2 Answers2

2

The answer is actually in your print statements.

System.out.println("Result for " + companyName +"not found in line:" + lineCord);

prints "Result for ..." on one line and the rest on the next one. Considering that you are not using printf, one can deduce that companyName has '\n' char at the end. Hence print which goes over two lines. As you noted the length of strings differs by 1, which again points to the fact that companyName variable may contain that new line character. Please refer to this answer on how to remove line breaks.

Community
  • 1
  • 1
AlmasB
  • 3,377
  • 2
  • 15
  • 17
1

contains uses equals rather than ==. Possible issues can be : 1. Encoding difference between strings 2. Equal override is needed if picked up from list. 3. A newline or space is added. Try trimming or removing new line chars. 4. Run in debug mode and see if u see something unusual in strings.

Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24