2

I am very new to Java, sorry if the question is too simple. I am trying to evaluate whether an array is part of the fibonacci sequence. I do not know how to return "true" value when the whole "for" loop does not break. Any ideas? Thank you in advance! This is what I have for now:

public boolean checkFibb(ArrayList<Integer> array1) {
    int i;
    int fibb;

    if (array1.size() < 3) {
        System.out.println("Your array is too short!");
    } else {
        for (i = 0; i <= array1.size() - 2; i++) {
            fibb = array1.get(i + 2) - (array1.get(i + 1) + array1.get(i));

            if (fibb != 0) {
                System.out.println("Elements are not part of the Fibonacci sequence.");
                break;
            } else {
                System.out.println("Elements are part of the Fibonacci sequence.");
            }
        }
    }

    return true;
}
1
  • 5
    instead of using break; inside the loop, do return false; Commented May 8, 2016 at 15:16

2 Answers 2

3

You always return true from the method. You should do something as follows:

public boolean checkFibb(ArrayList<Integer> array1) {
   int i;
   int fibb;
   boolean isFibb = true;

   if (array1.size() < 3) {
       System.out.println("Your array is too short!");
       isFibb = false;
   } else {
       for (i = 0; i <= array1.size() - 2; i++) {
           fibb = array1.get(i + 2) - (array1.get(i + 1) + array1.get(i));

           if (fibb != 0) {
                System.out.println("Elements are not part of the Fibonacci sequence.");
                isFibb = false;
                break;
           } else {
                System.out.println("Elements are part of the Fibonacci sequence."); 
           }
       }
   }
   return isFibb;
}
Sign up to request clarification or add additional context in comments.

Comments

2

What you're doing in your code is you're breaking the current iteration of the loop when you detect that the elements aren't part of a fibonacci sequence. break only stops the current iteration of the loop that you are in. What you want to do is return false from the function at this point. When you detect that the array is indeed a fibonacci sequence you would want to return true at this point.

If you array is too short, it cannot be a fibonacci sequence thus you would return false at this point.

public boolean checkFibb(ArrayList<Integer> array1) {
int i;
int fibb;

if (array1.size() < 3) {
    System.out.println("Your array is too short!");
    return false;
} else {
    for (i = 0; i <= array1.size() - 2; i++) {
        fibb = array1.get(i + 2) - (array1.get(i + 1) + array1.get(i));

        if (fibb != 0) {
            System.out.println("Elements are not part of the Fibonacci sequence.");
            return false;
        } else {
            System.out.println("Elements are part of the Fibonacci sequence.");
            return true;
        }
    }
}

}

2 Comments

Thanks for your answer! however something still seems to be wrong. I am editing my code in Eclipse and it shows that i++ part of my for-loop is a 'dead code'. What is more, the code is not working properly, with an array [2, 4, 6, 10, 15, 26] it returns true value, although 10+15 does not equal 26. How could that be? And one more question, don't I need some return value for my checkFibb function?
Yes I didn't completely correct the code, I was just showing the way to use return statements. The return type is indeed boolean. You will have to add another return statement at the bottom of the function in order for the compiler to be happy. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.