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

Thread: boolean problem.

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default boolean problem.

    public boolean 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
      {
      return true;
      }
      else {
      return false;
      }
     
      }
     
     
     
    }


    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.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: boolean problem.

    The code would be easier to read if it were properly formatted with nested statements indented 3-4 spaces.
    All the statements should NOT start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: boolean problem.

    Sorry I was doing this on a website. It messed up when I moved it here. But I fixed the problem.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Boolean or If?
    By 0ffConstantly in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2012, 11:32 AM
  2. Boolean Method Test Problem
    By Khadafi in forum Java Theory & Questions
    Replies: 4
    Last Post: January 13th, 2012, 11:07 PM
  3. Problem printing a two dimensional boolean array
    By sallyB in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 03:56 PM
  4. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  5. Some help with boolean
    By JPetroSS in forum Java Theory & Questions
    Replies: 3
    Last Post: November 2nd, 2010, 05:38 AM