Jump to content
  • Advertisement
Sign in to follow this  
Kevinator

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

This topic is 3752 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
I tried compiling both of the snippets. The first one compiled fine. The second one had an error because "return -100;" is an unreachable statement.

Share this post


Link to post
Share on other sites
Thanks, this confirms my suspicion that there is something wrong with my compile process rather than my code.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!