Rohinb97 Rohinb97 - 2 years ago 85
Java Question

Stuck in a loop of errors: unreachable statement and no return statement provided

Here is a snippet of my code:

public static int dc (String s,int k, int c){
String s1, s2;
int m, n;
if (check(s, k) != -1) {
int p = check(s, k);
s1 = s.substring(0, p) + s.substring(p + 1);
s2 = s.substring(0, p + 1) + s.substring(p + 2);
if ((check(s1, k) == -1) || (check(s2, k) == -1)) {
return c;
} else {
m = dc(s1, k, c);
n = dc(s2, k, c);
if (m > n) {
return n;
} else {
return m;

So the error says that there is a missing return statement (not sure which scenario have I missed). But when I add a return statement at the end, it shows that return statement unreachable. I'd found an helpful answer here: Unreachable return statement still throws error and I have tried putting it in a try catch block but it still asks for a return statement. Even though the compiler knows that anything I write in the end is redundant, it is still showing an error?

Answer Source

If your first check check(s, k) returns -1 then you don't return anything

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download