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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.