0

Can anyone tell me if im doing something wrong ?

public class Jafntekkijafnt
{
    public static void main(String[] args)
    {
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        int c = Integer.parseInt(args[2]);
        if (a == b) (b == c) (c == a)   System.out.println("equal");
        else                            System.out.println("not equal");
    }
}   

It's java programming
I'm trying create a program with three integers that prints equal if all are equal and not equal if they are not.

I know its the 8th line but don't how to get it right.

2
  • Looks like line 8 is invisible (like the rest of your code). Commented Sep 5, 2012 at 14:30
  • 2
    Using if(args[0].equals(args[1]) && args[1].equals(args[2])) may be all you need. Commented Sep 5, 2012 at 14:34

6 Answers 6

10

instead of if (a==b)(b==c)(c==a) use

if (a==b && b==c)

Note there is no need to write c==a if a==b and b==c hold

Sign up to request clarification or add additional context in comments.

2 Comments

I think equals is transitive.
@TheCat Yes, equals() is transitive(it's specified in its java doc) Interesting though - I can't find anything about == transitivity in the JLS specification so I'm feeling a bit foolish )))
0

THere is a problem with your "if" syntax. It should be

if (a == b && b == c && c == a) {
//...
}

Note that youare using primitives here and not object. If you were using Integer instead of int then the == would check if the objects were teh same and not if there values were the same. In this case you would need to use

if (a.equals(b) && b.equals(c) && c.equals(a)) {
//...
}

The && means logical and and is explained here

Comments

0

You have got the brackets a little bit wrong and you need to use the AND (&&) operator.

Comments

0

Not that it is strictly necessary in the one-line case, but you should also include brackets around your 'if' and 'else' statements. It's bad practice not to use them because it can lead to some unsuspected behavior. I know personally that I've gotten burned in the past over this issue. Plus it makes your code easier to read which is invaluable.

My code is below:

public class ThreeIntTest {
public static void main(String[] args) {
    int a = Integer.parseInt(args[0]);
    int b = Integer.parseInt(args[1]);
    int c = Integer.parseInt(args[2]);
    if ((a == b) && (b == c)) {
        System.out.println("The three integers are equal.");
    } else {
        System.out.println("The three integers are not equal.");
    }
} }

The && logical operator will tell the 'if statement' if both a==b AND b==c evaluate as true, if one or both are not true, then the statement is made false and you go directly to your 'else statement.'

Used here is the transitivity property of relations. Which says that if a = b and b = c, then it follows that a = c. This is the most effective way that I can think of comparing three integers.

Comments

0

Try this:

if(a == b && a == c && b == a && b == c && c == a && c == b){
...
}

Quick and easy.

Comments

0

You need to use && between the brackets () :

(a==b) && (b==c) && (c==a)

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.