0
public class cripto {   
    static String chiave, mex;
    static int lungC,lungM, lungAl,lungCh;
    static String[] aChiave, aMex;
    static String[] alfabeto = {"a", "b", "c","d", "e", "f","g", "h", "i","l", "m", "n","o", "p", "q","r", "s", "t","u", "v", "z"};
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    chiave=JOptionPane.showInputDialog(null, "Inserisci chiave di criptografia di lunghezza: "+alfabeto.length);
    System.out.println("chiave: "+chiave);
    lungC=chiave.length();
    for (int i=0; i<lungC; i++){
        aChiave=chiave.split("");
        System.out.println(i+": "+aChiave[i]);
    }
    mex=JOptionPane.showInputDialog(null, "Inserisci messaggio");
    System.out.println("messaggio: "+mex);
    lungM=mex.length();
    for (int i=0; i<lungM; i++){
        aMex=mex.split("");
        System.out.println(i+": "+aMex[i]);
    }
    lungAl=alfabeto.length;
    lungCh=aChiave.length;
    System.out.println("Lunghezza array alfabeto: "+lungAl);
    System.out.println("Lunghezza array chiave: "+lungCh);
    String[] aCripto=new String[lungM];

    if(aChiave.length==alfabeto.length){
        for(int i=0;i<lungM;i++){
            for(int j=0;j<alfabeto.length;j++){
                //System.out.println(i+": "+h+" - "+h1);
                if(aMex[i]==alfabeto[j]){
                    aCripto[i]=aChiave[j];
                }
            }
        }
    }else{
        JOptionPane.showMessageDialog(null, "Lunghezze diverse");
    }
}

}

Hy guys, I Made this simple criptography's program but I have the problem that in the for, the program doesn't enter in the "if" even if the equality is verified.

                if(aMex[i]==alfabeto[j]){
                    aCripto[i]=aChiave[j];
                }

can someone help me? sorry for my bad english.

V__
  • 538
  • 10
  • 20

2 Answers2

0

Man, you can't use == like this in Java! that operator check if the two objects point to the same memory reference... you have to use equals: if(aMex[i].equals(alfabeto[j]))

Community
  • 1
  • 1
Tommaso Bertoni
  • 2,333
  • 1
  • 22
  • 22
0

With Java Strings rember that == operator tests if a reference is the same whereas .equals() tests if the value is the same.

So in you case you should use .equals to test value equality. Have a look here for comprehensive information String compare in Java

Community
  • 1
  • 1
abarisone
  • 3,707
  • 11
  • 35
  • 54