lundi 27 juillet 2020

Although the two characters are the same, the condition behaves as if they are not the same in java [duplicate]

I am trying to count each character in strings. My function getting two strings.

when I debugging my code I saw a very strange thing for me. Although the two characters are the same, the condition behaves as if they are not the same. my expectation is when they are the same to increase the value of lcounbir.You can see the below

public class ContainStr {
    public static void main(String[] args){
        System.out.println(containString("hostr1ell","ehllo"));



    }

    public  static boolean containString(String str1,String str2){

        int str2lenght=str2.length();
        int str1lenght=str1.length();

        int[] str1ar=new int[str2lenght];
        String [] strbirar=new String[str1lenght];
        String [] strikiar=new String[str2lenght];
//        int vl=1;
//        int zerocounter=0;


        for(int i=0 ;i<str1lenght;i++){

            strbirar[i]=String.valueOf(str1.charAt(i));


        }
        for(int i=0 ;i<str2lenght;i++){

            strikiar[i]=String.valueOf(str2.charAt(i));


        }
        int lcounbir=0;
        int lcouniki=0;
        int contson=1;

        for (String letter:strikiar
             ) {
            for(int i=0;i<str2lenght;i++){

                if(letter==strikiar[i]){
                    lcouniki+=1;
                }
            }
            for(int i=0;i<str1lenght;i++){
                if(letter==strbirar[i]){
                    lcounbir+=1;
                }
            }
            if(lcounbir!=lcouniki){
                contson=0;
                break;

            }
        }
        if (contson==0){
            return false;
        }
        return true;

    }
}

enter image description here enter image description here

Aucun commentaire:

Enregistrer un commentaire