Triangle Type Program Is there any way to simplify it further?

import java.util.*;

class Triangle {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();


        if(a==b &amp;&amp; b==c)
            System.out.println("Equilateral");

        else if(a &gt;= (b+c) || c &gt;= (b+a) || b &gt;= (a+c) )
            System.out.println("Not a triangle");

        else if ((a==b &amp;&amp; b!=c ) || (a!=b &amp;&amp; c==a) || (c==b &amp;&amp; c!=a))
            System.out.println("Isosceles");

        else if(a!=b &amp;&amp; b!=c &amp;&amp; c!=a)
            System.out.println("Scalene");
    }
}
Is there any way to simplify it further?


Sure, but what's more important: Create some useful methods. Your class is single shot only: read data, classify, output. This is a bad start, a method should do one thing only. So you need three methods.

You classify into 4 categories and this is a clear case for an enum:

enum TriangleType {
    EQUILATERAL, INVALID, ISOSCELES, SCALENE
}


This is the only interesting method:

static TriangleType classify(int a, int b, int c) {
    if (a &lt;= 0 || b &lt;= 0 || c &lt;= 0) return INVALID; // added test
    if (a == b &amp;&amp; b == c) return EQUILATERAL;
    if (a &gt;= b+c || c &gt;= b+a || b &gt;= a+c) return INVALID;
    if (b==c || a==b || c==a)) return ISOSCELES;
    return SCALENE;
}


Note that I'm violating the "braces everywhere" rule. I can't help myself.

In case of ISOSCELES, there's no need for checks like a==b &amp;&amp; b!=c as we already know that not all sites are equal.

In case of SCALENE, there's no need for any test, as we know that there are no two equals sides.



You're using a bit too many parentheses, but feel free to keep them if you feel you need them.

Be consequent with your spacing, there are rules for it, which help to make it more readable:


single space between "if" and "("
(but no space between a method name and "(")
no space after "("
no space before ")"




The addition is subject to overflow as Ryan noted, but let's ignore it for now. You should know about it and do a corresponding test if needed, but in general, overflow is impossible to check for everywhere.

Here it's trivial, just replace a &gt;= b+c by a &gt;= (long) b+c. The latter means a &gt;= ((long) b) + c, which is equivalent to ((long) a) &gt;= ((long) b) + ((long) c).