所以我有一个程序正在根据我在CSV文件中的位置来格式化和查询google的经纬度坐标。该程序绝不是高效的,老实说我不在乎,只要它格式化了我可以用于XML文件的正确格式的输出即可。 Google对查询有限制,因此我决定将以前的查询存储在基本文本文件中,然后使用缓冲区读取器检查line.contains(companyname)是否匹配,但从未匹配。我同时打印出要与contains和该行进行比较的字符串,它们在打印输出中是相同的,但是长度相差1。我假设存在一些隐藏的字符或包含了contains()方法的东西。

样本输出:

java - 即使字符串看起来相同,String.contains(astring)也不会返回true-LMLPHP

以下是此过程的两种重要方法。

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;
}

最佳答案

答案实际上在您的打印报表中。

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


在一行上打印“ Result for ...”,在下一行上打印其余内容。考虑到您没有使用printf,可以推断出companyName结尾处有'\n'字符。因此打印超过两行。如前所述,字符串的长度相差1,这再次表明companyName变量可能包含该换行符。请参考this答案,了解如何删除换行符。

10-01 19:16
查看更多