#Need help In java.

1 messages · Page 1 of 1 (latest)

open raptor
#

Hi need help in java code, Here is the Java Code.

class LogicalOperators {
    public static void main (String [] args) {
        int value1 = 1;
        int value2 = 2;

        if ((value1 == 5) && (value2 == 2));{

            System.out.println("Correct");
        }

        if ((value1 == 1) || (value2 == 1));{

            System.out.println("XX");
        }
    }
}

Kindly answer me in personal chat or here.

torn socketBOT
#

<@&987246399047479336> please have a look, thanks.

acoustic timber
#

you didnt ask anything yet

open raptor
#

There are eigth warrnings and i didn't understand that, Can you help me with that?

acoustic timber
#

share them

open raptor
acoustic timber
#

the problem is the ; before the { of the if

#

remove them

open raptor
#

so remove the simi colon?

acoustic timber
#

in the if, yea

open raptor
#

Now it shows 7 warrnings and another thing is that some of code is dim. just like in the screenshot

stray steppe
#

Read what the warnings say

open raptor
#

I didn't understand that but these are the warnings.

stray steppe
#

Take first two warnings

#

Do you understand them?

open raptor
#

I understand the English but how to fix that? I don't know.

#

the is the screenshot of my code.

stray steppe
#

Well, for example you defined value1 to be 1

#

Therefore, comparing it to 5 will always report false

open raptor
#

But I want to apply function and print the output

stray steppe
#

In that case, whatever is in "if" statement must be true

open raptor
#

Ok and what about the second if statnment

stray steppe
#

&& is a logical AND, if statement to the left is false then the entire expression is false and the right statement won't even be checked

open raptor
#

And can you Explain why must be true?

stray steppe
#

In the second statement comparing value1 to 1 will always result in true, so the part after || won't run as well

stray steppe
open raptor
stray steppe
#

Then you need to add the else block

open raptor
#

how add else block?

open raptor
stray steppe
#

else { code }

open raptor
stray steppe
#

Because the if statement is always true

open raptor
#

but i wanted to add else statement too.

prime hedge
#

You have an else, but you can never enter it. Thus it's shown that way.

glass sphinx
#

@open raptor basically

#

your program is fine

#

but the values are constants

#

so the thing giving warnings can tell you that the ifs are pointless

#

if you got value1 and value2 from an external source - say user input - then it wouldn't complain

inner furnace
#

Or just pull it out to your own method.

public static void main(String[] args) {
    doTheThing(1, 2);
}

public static void doTheThing(int value1, int value2) {
    if (value1 == 5 && value2 = 2) {
        System.out.println("Correct");
    }

    if (value1 == 1 || value2 == 1) {
        System.out.println("XX");
    }
}

The compiler will now know that the values could be different.

If doTheThing were private, a determined analyser could still conclude you never call it with values that would reach some code paths.