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.

boolean problem.

publicboolean sum28(int[] nums){int[] sum28 ={10,2,2,2,2,50};int sum;for(int i=0; i<sum28.length; i++)//This will cycle through the elements{if(sum28[i]==2)//If the value contained in sum28[28] is a 2 it will add that to the sum{
sum = sum +2;//This adds a 2 to the sum IF the element has a 2}else sum = sum +0;//it it does not have a 2 it adds 0 to make sure only the two's are added.if(sum ==8)//If sum = 8 than it will return true if not it will return false{returntrue;}else{returnfalse;}}}

Help please. When I complie this code it is telling me it isn't returning a boolean value even though I am telling it to return a true/false. please explain why I can't get it to compile.

--- Update ---

My error is this:Error: public boolean sum28(int[] nums) {
^^^^^^^^^^^^^^^^^
This method must return a result of type boolean

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.

Re: boolean problem.

What do you want this code to do? As of now it's going to return false every time. I don't think that's what you want to do.

But the compiler error is occurring because the compiler isn't smart enough to know that the for loop will be executed. In theory, it's entirely possible for a for loop's initial condition to not be met, in which case you'll reach the end of the method without returning anything.

Re: boolean problem.

The formatting is still off. The code would be easier to read if it were properly formatted with nested statements indented 3-4 spaces.
There should NOT be two }}s one above the other.
It's not possible easily to see the } that ends the for loop