Sign in to follow this  
Kevinator

[java] The following code won't compile because...

Recommended Posts

"This method must return a result of type int Possible problem: the if-statement structure may theoretically allow a run to reach the end of the method without calling return. Consider using a final else {... to ensure that return is always called." My question is... why? My code must return an int as far as I can tell.
private int getNumber(String str, int index) {
  String estr=""+str.charAt(index);
  if(!Character.isDigit(str.charAt(index)))
    return -1;
  else {
    index++;
    while(index<str.length()&&Character.isDigit(str.charAt(index))) {
      estr+=str.charAt(index);
      index++;
    }
    return Integer.parseInt(estr);
  }
}

The following variation results in the same problem:
private int getNumber(String str, int index) {
  String estr=""+str.charAt(index);
  if(!Character.isDigit(str.charAt(index)))
    return -1;
  else {
    index++;
    while(index&lt;str.length()&&Character.isDigit(str.charAt(index))) {
      estr+=str.charAt(index);
      index++;
    }
    return -50;
  }
  return -100;
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this