I'm working on a Java program that takes an input String called input which looks like
'(A A A A B C C D 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)?
private static boolean func2(String input){ /*input: '(A A A A B C C D E E E E)
input = input.replace("'","");//replace to null char
input = input.replace("(","");//replace to null char
input = input.replace(")","");//replace to null char
System.out.println(input);
String[] items = input.split(" |\u0000",0);
System.out.println("Array items "+Arrays.toString(items));
int position = 0;
int countlength = 0; for (String z: items)countlength += 1;//determines the MAX length of the letters array
String checkagainst = "";
String[] letters = new String[countlength];
int[] count = new int[countlength];
System.out.println("\n Letters: "+Arrays.toString(letters)
+"\nCount: "+Arrays.toString(count)
+"\nItems: "+Arrays.toString(items));
for (int x = 0; x < items.length; x++){
System.out.println("\n\nCheck if"+checkagainst+"=="+items[x]);
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");
}
}