jeudi 5 février 2015

Why does A == A evaluates to false?

I'm working on a Java program that takes an input int[] called items which looks like



[A,A,A,A,B,C,C,D,A,A,E,E,E,E]


And my output, A String[] called Letters that should look like



[A,B,C,D,A,E]


And another Array, an int[] called count that should look like



[4,1,2,1,2,4]


And I have code that looks like below, but for some reason, every time I run it it won't work. The print statements read that it doesn't ever go to the if statement. Line 2 on the second loop reads:



Check if A == A



Then it goes to the else statement and prints



A != A



What am I doing wrong? Why is A (items[0]) not equal to A (checkagainst)?



for (int x = 0; x < items.length; x++){
System.out.println("\n\nCheck if "+checkagainst+" == "+items[x]);//line 2
if (checkagainst == items[x]){
count[position] += 1;
System.out.println("Iteration:" + x
+"\n Letters: "+Arrays.toString(letters)
+"\n Count: "+Arrays.toString(count)
+"\nSwitch: if");
}
else{
System.out.println("checkagainst != items[x]");
letters[position] = items[x];
count[position] = 1;
checkagainst = items[x];
position ++;
System.out.println("Iteration:" + x
+"\n Letters: "+Arrays.toString(letters)
+"\n Count Arrays"+Arrays.toString(count)
+"\ncheckagainst: "+ checkagainst
+"\nSwitch: else");
}
}

Aucun commentaire:

Enregistrer un commentaire