I am trying to compare two lists to one another. Loop through each index in both lists and compare the values to each other.
1. One position does not match: if loop1[i] != loop2[j] and i ==j, value at i in loop1 does not match value in loop2.
2. match but not at position: loop1[i]== loop2[j] and i!=j, value is common in both loops but is at wrong position.
3. if the first occurs more than once, multiple values dont match.
Problem is when i test it, i get the wrong output e.g loop 1 = 1,2,3,4,5,6,7 and loop2 = 1,2,3,5,5,7. It prints the first outcome. Correct
problem is here:
loop1 = 1, 2, 3, 4, 5, 6, 7
loop2 = 1, 2, 4, 3, 5, 6, 7, supposed to print second outcome but prints first outcome instead.
Eg
selectedindices = List.of(1, 2, 3, 4, 5, 6)
indicesofpositionguessed = List.of(1, 2, 3, 5, 5, 6
when you compare the two, its supposed to be: "User guess at position 3 is correct but at wrong position" but instead is "User guess at position 3 did not match selected"
Also for some reason the method returns a string but does not recognize the returned strings in the for loop but makes me put a return statement outside of the for loop. Why? Is there any way to get around it?
What I have tried:
public String guessPosition(List<Integer> indicesOfPositionGuessed) {
int count = 0;
for (int i = 0; i < indicesOfPositionGuessed.size(); i++) {
Integer item1 = indicesOfPositionGuessed.get(i);
for (int j = 0; j < selectedIndicies.size(); j++) {
Integer item2 = selectedIndicies.get(j);
if (i != j) {
if (item1.equals(item2) == true) {
return "User guess at position " + i + " is correct but at wrong position";
}
}
if (i == j) {
if (item1.equals(item2) == false) {
count++;
return "User guess at position " + i + " did not match selected";
}
}
}
}
if(count > 0)
return "User guess did not match in two positions";
return null;
}