# How to Check If a Triangle Can be Formed in Java

The sides of a triangle is constrained by the rule that the sum of any of its two sides is always bigger than the third side. Using this rule, we can find whether 3 sides given for a triangle is valid or not. The following sample Java program checks whether the given 3 sides can form a valid triangle.

```import java.util.Scanner;

/**
* Validates whether the given 3 sides can form a triangle or not.
* @author jj
*/
public class ValidTriangle {

public static void main(String[] args) {

Scanner sn = new Scanner(System.in);
System.out.println("Enter length of first side of triangle:");
double a = sn.nextDouble();

System.out.println("Enter length of second side of triangle:");
double b = sn.nextDouble();

System.out.println("Enter length of third side of triangle:");
double c = sn.nextDouble();

ValidTriangle vt   = new ValidTriangle();
if(vt.isTriangleValid(a, b, c)) {
System.out.println("Sides entered can form a triangle!");
}else {
System.out.println("Sides entered cannot form a triangle!");
}

}

/**
* Checks whether the 3 sides can form a triangle
* The sum of any two sides must be greater than the other side
* @param a
* @param b
* @param c
* @return
*/
private boolean isTriangleValid(double a, double b, double c) {
if((a+b)>c && (a+c)>b && (b+c)>a) {
return true;
}else {
return false;
}
}
}```