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 4 of 4

Thread: Need help with a recursion..

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Need help with a recursion..

    Hello fellows.. In the second method "searchArray2" it shows me an error on "return false", which says that it's incompatible types.. I know that it's about mixing string and int, but what if I want my array to be int, how can I return true if the value is found, false otherwise.
    Thank you

    public class RecursiveCalculator
    {
        public int searchArray(int[] array, int value){ 
            if (value != array.length)
                return value;
            else 
                return -1;
        }
     
        public String searchArray2(String[] array, String value){ 
            if (value != array.length)
                return false;
            else 
                return true;
        } 
     
        public String reverseString(String reverse){
            if (reverse.length() == 0) 
                return reverse;
     
            return reverseString(reverse.substring(1)) + reverse.charAt(0);
        }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need help with a recursion..

    The issue is that the return type of the method is String but you are attempting to return a boolean. This is the same thing:
    String result = true; // error incompatible types
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a recursion..

    So what am I supposed to do?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need help with a recursion..

    Return a String or change the return type in the method signature.
    Improving the world one idiot at a time!

Similar Threads

  1. Recursion
    By maple1100 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2013, 01:49 AM
  2. Recursion Help
    By WeaKeN in forum Algorithms & Recursion
    Replies: 10
    Last Post: May 27th, 2011, 08:17 AM
  3. [SOLVED] Recursion help
    By Actinistia in forum Java Theory & Questions
    Replies: 3
    Last Post: March 21st, 2011, 12:26 PM
  4. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 03:42 PM
  5. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 1st, 2010, 11:27 PM