Java - Compare Elements in an Array to Each Other | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java - Compare Elements in an Array to Each Other

Hi all, In test cases, my code returns true for everything, even when false is expected. Can anyone point out why? As an example, returns true for this: @Test public void test10() { assertThat(Challenge.testJackpot(new String[]{"SS", "SS", "Ss", "Ss"}), is(false)); } or this: @Test public void test8() { assertThat(Challenge.testJackpot(new String[]{"hee", "heh", "heh", "heh"}), is(false)); } I've tried a few different things but keep getting the same results. TIA! public class Challenge { public static boolean testJackpot(String[] result) { for (int i = 0; i < result.length; i++){ if (!(result[i].equals(result[i]))) return false; } return true; } }

14th Jul 2020, 3:11 PM
GG128
1 Answer
+ 4
result[i].equals(result[i]) Here you are comparing result[i] with itself. That's why your method returns always true.
14th Jul 2020, 3:16 PM
Denise Roßberg
Denise Roßberg - avatar