简体   繁体   English

Java 没有正确比较两个相等的字符串

[英]Java not comparing correctly two equal strings

I'm confused as this shouldn't be an issue, I load data from a file to a List of classes and then I check from user input - a string.我很困惑,因为这应该不是问题,我将数据从文件加载到类列表,然后从用户输入中检查 - 一个字符串。 If the string is equal when comparing, then a few variables extracted from the file should be printed.如果比较时字符串相等,则应打印从文件中提取的一些变量。

public static Double[] buscarLocacion(String estado,String municipio){

    Double[] coord = { 0.0, 0.0 };       
    for(Ubicacion i: ubicacion) {

        System.out.println("check: "+estado +" getEstado:"+ i.getEstado() + " ==: " +i.getEstado().equals(estado));

        if(i.getEstado().equals(estado)) {
            if(i.getMunicipio().equals(municipio)) {

                coord[0] = i.getLat();
                coord[1] = i.getLon();
                return coord

        //etc..

I made a separate class with a main function.我创建了一个带有 main 函数的单独类。 This works as expected, the program prints this when the string is equal:这按预期工作,程序在字符串相等时打印:

found: hidalgo - tula de allende lat:20.0535516 lon: -99.3395636

However when I import this class into another, when doing the comparison, I always get null.但是,当我将这个类导入另一个类时,在进行比较时,我总是得到 null。 I printed what is comparing and if the program founds the comparison is true or false like this:我打印了比较的内容,如果程序发现比较是真还是假,如下所示:

System.out.println("check: "+estado +" getEstado:"+ i.getEstado() + "  equals?=: " +i.getEstado().equals(estado));

and this code prints this:这段代码打印了这个:

check:  yucatan file:veracruz de ignacio de la llave ==: false
check:  yucatan file:yucatan ==: false //<<<- how is this possible??
check:  yucatan file:oaxaca ==: false

Any ideas why this is happening?任何想法为什么会发生这种情况? I can not find the problem.我找不到问题所在。 What am I doing wrong here?我在这里做错了什么?

Your input may contain whitespace.您的输入可能包含空格。 For example, " yucatan" .例如, " yucatan"

Probably the problem is with the "i" or with heading or trailing whitespace.问题可能出在“i”或标题或尾随空格上。 Try the following:请尝试以下操作:

public static  Double[] buscarLocacion(String estado,String municipio){

    Double[] coord = { 0.0, 0.0 };

     for(Ubicacion i: ubicación){
         String iEstado = i.getEstado().trim();
         estado = estado.trim();

         System.out.println("check: "+estado +" getEstado:"+ iEstado + " ==: " +iEstado.equals(estado));


            if(iEstado.equals(estado)){
                String iMunicipio = i.getMunicipio().trim();
                municipio = municipio.trim();

                if(iMunicipio.equals(municipio)){

                coord[0] = i.getLat();
                coord[1] = i.getLon();
                 return coord

//etc..

I have had this kind of errors before, and although I am not sure why this happens, this should fix your code.我以前遇到过这种错误,虽然我不确定为什么会发生这种情况,但这应该可以修复您的代码。 If it does, you can try to change the order of the comparison:如果是,您可以尝试更改比较的顺序:

estado.equals(i.getEstado());

and check if this way it works (not to have to create additional variables).并检查这种方式是否有效(不必创建额外的变量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM