0

How do I get two variables (boolean and string) into one if statement? All I was able to do is:

if (shop[i].getLot() == true) { 
    totalrenters += 1;
}

Then I'm totally blank. How do you get the if .getString("double") to work together with the .getLot in the statement so it can count and display the renter's info?

1
  • If your string is MyString for example you can if (shop[i].getLot().equals('MyString')) if shop[i].getLot() is actually a boolean just use if (shop[i].getLot()) Commented Mar 12, 2015 at 5:50

4 Answers 4

1

You can combine multiple boolean tests with && or ||. In your case

if (shop[i].getLot()  &&
    "double".equals(shop[i].getStorey())) {
        totalRenters++;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this but using && in your if statement like

if (shop[i].getLot() && shop[i].getStorey("single")) {
    System.out.println ("Hey, this is a single storey corner shop");
}

Comments

0

Remember that the overall if statement is looking for a boolean expression in the end. That is to say, whatever is in between those parentheses must evaluate to true if anything is to execute inside of the if block.

Now, to your question. If you say that getLot() represents whether or not it's a corner lot, then you're halfway there.

if(shop[i].getLot()) {
    // logic
}

You can omit the == true portion; you'd only be comparing booleans to booleans at that point, which is not necessary.

We want to add the string condition next. I'll make this excessively simple, but there are more clever ways to do this, such as regular expressions.

If we are saying that a method getStorey() exists that returns String, then we can check to see if that particular string is equal to "double". You can use the logical AND (&&) operator to combine more than one boolean condition.

if(shop[i].getLot() && "double".equals(shop[i].getStorey())) {
    // increment a counter here
}

Comments

0

Try below:

if (( "double".equals(shop[i].getStorey() ) && ( shop[i].getLot() )) {
            totalRenters++;
        }

Note: shop[i].getLot() return true|false so is directly working as if(true|false)


You can Add as many as condition in if statement base on your requirement

Eg:

String name = "Hello"
boolean isTrue = true;

Use of && in if statement:

If you want to do something when (name is Hello and isTrue = true)

if("Hello".equals(name) && isTrue)


Use of || in if statement:

If you want to do something when (name is Hello or isTrue = true)

if("Hello".equals(name) || isTrue)

Comments

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.