Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Unreachable Statement in simple recursion code

  1. #1
    Junior Member
    Join Date
    May 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Unreachable Statement in simple recursion code

    i'm practicing recursion problems,
    That's the question :

    Given a string, compute recursively (no loops) the number of lowercase 'x' chars in the string.


    countX("xxhixx") → 4
    countX("xhixhix") → 3
    countX("hi") → 0

    that's my code :

     
    public int countX(String str) {
      if (str.length() == 0);
        return 0;
      if (str.charAt(0) == 'x') // <---- unreachable statement here
     
          return 1 + countX(str.substring(1));
      return countX(str.substring(1));
    }
    the 4th line has unreachable statement error
    i can't find a reason why is it unreachable because the first statement is wrapped in an if statement :/

    i'll appreciate any help
    thanks !
    Last edited by omerben; May 12th, 2021 at 02:50 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Unreachable Statement in simple recursion code

    The return statement immediately before the unreachable statement will cause execution to exit the method.

    Add the -Xlint option to the compiler command line to get an important warning about the if statement.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unreachable Statement in simple recursion code

    I accidently typed ; at the of first if statement, that was the problem.
    Thanks !

Similar Threads

  1. Unreachable statement problem
    By Javafresh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 2nd, 2018, 05:50 AM
  2. 46: Unreachable Statement
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 7th, 2011, 07:42 AM
  3. another unreachable statement..
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2009, 12:15 PM
  4. unreachable statement Again?
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 1st, 2009, 10:23 AM
  5. unreachable statement?
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: September 11th, 2009, 10:06 AM