String[] testing = new String[]{"Hat", "Cat", "Bear", "Tiger", "Bird"};
Using the array above and generating it in a random order, I'm trying to check each String to the other, and if any are the same, then checking goes up by 1.
Ex: if testing[0] == testing[1], checking++, if not, test[0] == test[2] and so on.
However, I'm just not getting my logic right as its giving me random integers that I'm not expecting. If anyone can point out my errors id appreciate it.
Note: I'm new to Java and do not know a lot of methods out there.
public int checkUp() {
int checking = 0;
int looper = 0;
int checker = 0;
for (int i = 1; i < testing.length; i++) {
while (checker < testing.length) {
if (String.valueOf(testing[looper]).equals(String.valueOf(testing[i]))) {
checker++;
} else {
looper++;
}
checker++;
}
}
return checking;
}
return checking;but you never do anything with that variable beside initializing it withint checking = 0;. So I'm surprised you say you are getting "random integers" when all you should ever get returned from that method is a very non-random0for (int i...) { for (int j...) { if (testing[i] is equal to testing[j]) { checking++; } } }return testing.length - (int) Stream.of(testing).distinct().count();