Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you check what is wrong with my code? I try to pass this public test and I c

ID: 3721690 • Letter: C

Question

Can you check what is wrong with my code? I try to pass this public test and I can't.

@Test
public void testSplitDash() {
String a = "yarns--and";
String[] arr = WordLib.splitDash(a);
assertEquals("yarns", arr[0]);
assertEquals("and", arr[1]);

String b = "single-dash";
String[] arr2 = WordLib.splitDash(b);
assertEquals("single-dash", arr2[0]);
assertEquals("", arr2[1]);
}

Here is the code:

public static String[] splitDash(String w) {

if (w.endsWith("-")) {

String t [] = new String [w.split("-").length + 1];

t [t.length - 1] = "";

for (int i = 0; i < t.length - 1; i++) {

t[i] = w.split("-")[i];

}

return t;

}

else {

return w.split("--");

}

}

}

Explanation / Answer

From the assertion given above:

public static String[] splitDash(String w) {
if (w.endsWith("-")) {
String t[] = new String[w.split("-").length + 1];
t[t.length - 1] = "";
for (int i = 0; i < t.length - 1; i++) {
t[i] = w.split("-")[i];
}
return t;
} else {
String t[] = w.split("--");
if (t.length == 1) {
String s[] = {t[0], ""};
return s;
}
return t;
}
}

If this doesn't work share what splitDash should actually do.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote