2

I am pretty new to Java what I am trying to do may seem really strange but it is a matter of me understanding a little bit about how Java works more than actually accomplishing a set result.

If I have a boolean method for instance:

public class doThings
{

    // imagine the intial value of this variable is set
    // and or modified using getters and setters between the declaration
    // and the following method
    private boolean boolVar;


    public boolean doSomething()
    {
        if(boolVar == true)
        {
            //does things and then returns true
            return true;
        }
        else
        {
            return false;
        }
    }
}

And I want to call this method in another class like so...

public class testDoThings
{

    doThings someObject = new doThings;
    public static void main(String[] args)
    { 
        someObject.doSomething()
    }
} 

How do I check (in the class testDoThings) to see if the method has returned true or returned false and print messages accordingly something like this...

public class testDoThings
{

    doThings someObject = new doThings;
    public static void main(String[] args)
    {

        someObject.doSomething()
        if (doSomething() returns true) // yes I am aware this is not
                                        //valid
        { 
            // print a statment
        }
        else if (doSomething() returns false) // yes once again not valid
        {
            // print a different statement           
        }
    }
} 

I am aware that you could put these messages in the class containing the method but if I want different messages depending on where the method is called and what it is a called on then putting things in the original class method is not always going to work.

If I am completely off the track here please let me know, if someone can explain this to me though, that would be great!

0

5 Answers 5

3

You can try like this:

if (someObject.doSomething()) //So if your function returns true then the below statement will be executed
{ 
   // print a statment
}
else //This will check for the false condition of your function
{
   // print a different statement           
}
Sign up to request clarification or add additional context in comments.

1 Comment

Which would be the same as if(someObject.doSomething() == true){ }else{ }. For more operators check out this site: link
1

You can try something like this:

if(someObject.doSomething()){
    System.out.print("foo1");
}

else{
    System.out.print("foo2");
}

Comments

0

Here's one way:

if(someObject.doSomething() == true){
    System.out.print("foo1");
}

else{
    System.out.print("foo2");
}

Comments

0

Generally you compare two things using == operator: if (x == y) ... so we have:

if ( someObject.doSomething() == true ) {
    //statements
} else {
    //statement for the case where result of method call is false
}

BTW instead of if(x == true) you can simply write if(x).

Comments

-1

Conditional structures like if, while, do..., etc receive a boolean value, so it isn't necessary to put "boolVar == true". Just doing "if (boolVar)" is enough. As for your example in the doSomething method, just doing "return boolVar;" would do the work, without the need of any ifs, unless you pretend to do some more things on it.

To check a function return value works in the same way. I mean, variables have a value and functions also, the only difference is that variables hold a value while functions calculate or generate a value. So, in your code:

public class testDoThings {
    public void check() {

       doThings someObject = new doThings();

        boolean flag = sameObject.doSomething();
        if (flag) {

            // print a statment

         } else {
           //If you want to check falsehood, !flag would do.
                             // Notice the "!" sign before 
                             // the flag variable?
                             // It is a NOT sign, so
                             // NOT true = false
                             // NOT false = true
                             // The if will execute its code 
                             // When the result value is true
                             // So when the function return val is                           
                             // false.

            // print a different statement           
        }
    }
}

I hope this explanation is enough clear.

6 Comments

Why do you need an "else..if"? Since the condition (method call) is boolean, a simple else would suffice. Also, you don't, probably, want to call the same method twice, just to check the return.
This is potentially bad as it may cause doSomething to actually be executed twice.
I agree. to both comments. I am professional coder. I just tried to fill the holes. All can be improved. Let me fix it.
I don't understand how can people downvote the more explanatory and the best understandable for newbies in here. But hey, that's OK. Downvote if you fell like it.
They propably downvoted it before you edited your post. But anyway, as for now, youre code still wouldnt compile, because you have to wrap it in a method. If you fix it and also your comments youll get an upvote.
|

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.