Accessing variable in IF block to return it from method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javaMed
    New Member
    • Mar 2010
    • 6

    Accessing variable in IF block to return it from method

    Hello,

    I am working on an assignment and have run into a small problem. I have a method that returns a boolean value. My problem is that scope issues won't let me access the initialized value. Below is my method:

    Code:
     
    public static boolean compare(int st1, int st2, int fin1, int fin2)
        {
            boolean overlaps;
    
            if( ((st1 < st2)&&(st2 < fin1)) ||  ((st1 < fin2)&&(fin2 < fin1)) )
                overlaps = true;
            return overlaps;
        }
    the error that comes up if i put the return statement inside the IF block is that it is out of reach...but leaving it like this and I get the error that it has not been initialized...

    Help Please!
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    i would initialize overlaps in the beginning as true and don't forget to use else

    Comment

    • javaMed
      New Member
      • Mar 2010
      • 6

      #3
      I have now done so: overlaps is initialized as true to begin with and I have added an else statement, yet this method is constantly returning false - which only means it's my if statement --> it is logically false

      I know it's not apart of my question, but could you (or anyone) help me with the IF statement -- it is supposed to check for an overlap between values (interval scheduling)

      This is the logic my professor gave me to use, but it does not seem to work...

      Comment

      • javaMed
        New Member
        • Mar 2010
        • 6

        #4
        Code:
        public static boolean compare(int st1, int st2, int fin1, int fin2)
            {
                boolean overlaps = true;
        
                if( ((st1 < st2)&&(st2 < fin1)) ||  ((st1 < fin2)&&(fin2 < fin1)) )
                    overlaps = true;
                else
                    overlaps = false;
                return overlaps;
            }

        Comment

        • pbrockway2
          Recognized Expert New Member
          • Nov 2007
          • 151

          #5
          Are you still having trouble with this. Ie does the last posted boolean expression do what you want?

          If not what values does it fail for? And are we allowed to assume st1<fin1 and st2<fin2?

          Comment

          • javaMed
            New Member
            • Mar 2010
            • 6

            #6
            oh yeah, THANKS for all the help people

            I have solved my problem, it wasn't the IF statement, it was other pieces of my code in which I needed to increment some things and decrement others...

            But thanks anyway

            Comment

            • pbrockway2
              Recognized Expert New Member
              • Nov 2007
              • 151

              #7
              Great. I'm glad you've got it doing what you want.

              Just a little point of style: rather than using if/else to set a boolean variable you can just assign the expression to the variable. And in fact in your case you can use the expression as the return value.

              So what you posted is equivalent to

              Code:
              public static boolean compare(int st1, int st2, int fin1, int fin2)
                  {
                      return ((st1 < st2)&&(st2 < fin1)) ||  ((st1 < fin2)&&(fin2 < fin1));
                  }

              Comment

              • javaMed
                New Member
                • Mar 2010
                • 6

                #8
                Thanks A lot -- wow! that makes so much more sense...so much smaller/quicker

                =)

                Comment

                Working...