-2

I know there have been Java questions about triangles on here already, but I've tried looking and can't find a solution to my problem.

I'm working on a homework assignment where I need to provide the lengths of the sides of the triangle (which the user can provide in any order.

I have to find the type of the triangle (right, isosceles, etc) except for scalene since that wasn't in the instructions. I also have to find the area of the triangle.

I have over a hundred lines but the still doesn't work at all. There aren't any errors that prevent it from compiling; it just doesn't work properly when executed.

Any help would be much appreciated.

import javax.swing.JOptionPane;

public class TriangleChecker {

    public static void main(String[] args) {

        Boolean triangle, right, equilateral, isosceles;
        triangle = false;
        right = false;
        equilateral = false;
        String side1, side2, side3;
        double s1, s2, s3, perimeter, areai, bi, hi, fhi, be, he, fhe, areae, br, hr, arear;
        System.out.println("Hello welcome to the Triangle Checker");
        side1 = JOptionPane.showInputDialog("Please enter side 1 of the triangle.");
        side2 = JOptionPane.showInputDialog("Please enter side 2 of the triangle.");
        side3 = JOptionPane.showInputDialog("Please enter side 3 of the triangle.");
        s1 = Double.parseDouble(side1);
        s2 = Double.parseDouble(side2);
        s3 = Double.parseDouble(side3);
        if ((s1 > s2 + s3) || (s2 > s1 + s3) || (s3 > s1 + s2)) {
            triangle = false;
        }
        else {
            triangle = true;
        }
        if ((s1 > s2 && s1 > s3) && (s1*s1 == s2*s2 + s3*s3)) {
            right = true;
        }
        else if((s2 > s1 && s2 > s3) && (s2*s2 == s1*s1 + s3*s3)) {
            right = true;
        }
        else if((s3 > s1 && s3 > s2) && (s3*s3 == s1*s1 + s2*s2)) {
            right = true;
        }
        else {
            right = false;
        }
        if((s1 == s2) && (s1 == s3)) {
            equilateral = true;
            right = false;
            isosceles = false;
        }
        else {
            equilateral = false;

        }
        if((s1 == s2) && (s1 != s3)) {
            isosceles = true;
        }
        else if((s1 == s3) && (s1 != s2)) {
            isosceles = true;
        }
        else if((s2 == s3) && (s1 != s3)) {
            isosceles = true;
        }
        else {
            isosceles = false;
        }
        if((isosceles = true) && (s1 == s2)) {
            bi = (s3/2);
            hi = ((s1*s1) - (bi*bi));
            fhi = Math.sqrt(hi);
            areai = bi * fhi;
        }
        else if((isosceles = true) && (s1 == s3)) {
            bi = (s2/2);
            hi =((s1*s1) - (bi*bi));
            fhi = Math.sqrt(hi);
            areai = bi * fhi;
        }
        else if((isosceles = true) && (s2 == s3)) {
            bi = (s1/2);
            hi = ((s2*s2) - (bi*bi));
            fhi = Math.sqrt(hi);
            areai = bi * fhi;
        }

        else { 
            bi = 0;
            hi = 0;
            fhi = 0;
            areai = bi * fhi;
        }
        if(equilateral == true) {
            be = (s1/2);
            he = ((s2*s2) - (be*be));
            fhe = Math.sqrt(he);
            areae = be*he;
        }
        else {
            be = 0;
            he = 0;
            fhe = 0;
            areae = 0;
        }
        if((right = true) && (s1 < s2) && (s1 < s3) && (s2 < s3)) {
            br = (s1/2);
            hr =(s2);
            arear = br*hr;
        }
        else if((right = true) &&(s1 < s2) && (s1 < s3) && (s3 < s2)) {
            br = s1/1;
            hr = s3;
            arear = br*hr;
        }
        else if((right = true) && (s2 < s1) && (s2 < s3) && (s1 < s3)) {
            br = s2/2;
            hr = s1;
            arear = br*hr;
        }
        else if((right = true) && (s2 < s1) && (s2 < s3) && (s3 < s1)) {
            br = s2/2;
            hr = s3;
            arear = br*hr;
        }
        else if((right = true) && (s3 < s1) && (s3 < s2) && (s1 < s2)) {
            br = s3/2;
            hr = s1;
            arear = br*hr;
        }
        else if((right = true) && (s3 < s1) && (s3 < s2) && (s2 < s1)) {
            br = s3/2;
            hr = s2;
            arear = br*hr;
        }
        else {
            br = 0;
            hr = 0;
            arear = 0;
        }


        perimeter = s1 + s2 + s3;
        if(triangle = true) {
            System.out.println("This is a triangle.");
        }
        else {
            System.out.println("This does not equal a triangle.");
            System.exit(0);
        }
        if (right == true) {
            equilateral = false;
            isosceles = false;
        }
        else if (equilateral == true) {
            right = false;
            isosceles = false;
        }
        else {
            equilateral = false;
            right = false;
        }
        if(right = true) {
            System.out.println("This is a right triangle.");
        }
        else {
            System.out.println("This is not a right triangle.");
        }
        if (equilateral = true) {
            System.out.println("This is an equilateral triangle.");
        }
        else {
            System.out.println("This is not an equilateral triangle.");
        }
        if (isosceles = true) {
            System.out.println("This is an isosceles triangle.");
        }
        else {
            System.out.println("This is not an isosceles triangle.");
        }
        if ((arear == 0) && (areae == 0)) {
            System.out.println("The area of the triangle is " + areai + ".");
        }
        else if ((areai == 0) && (arear == 0)) {
            System.out.println("The area of the triangle is " + areae + ".");
        }
        else {
            System.out.println("The area of the triangle is " + arear + ".");
        }
    }
}
user1118321
  • 25,567
  • 4
  • 55
  • 86
  • 1
    First thing I see is a ton of if statements using assignment operator instead of comparison. For example `if (isosceles = true)` should probably be `if (isosceles == true)` or even better just `if (isosceles)`. Check out [my answer](http://stackoverflow.com/questions/21356277/java-if-statement-after-while-loop-not-working/21356308#21356308) to a different question here about that. – takendarkk Sep 10 '14 at 03:55
  • "There aren't any errors to make it where it won't compile it just doesn't work properly when executed." Stack Overflow is not your personal debugging team. Start by explaining what goes wrong, what you have tried, and what *specific* trouble you are having in fixing or understanding it. – Shashank Sep 10 '14 at 03:59
  • sorry if i misused the website. I just made an account because I was struggling so much. and Thanks for the tip Takendarkk. – Lee__Roberts Sep 10 '14 at 04:11

1 Answers1

-1

This sort of side-steps your question entirely -- I haven't really dug into your code and so don't know why it's failing, but you might be able to cheat a little and use math to find the area using Heron's formula and let you simplify your code.

Heron's formula will let you provide the lengths of the 3 sides of any triangle (right, equilateral, or scalene) and compute the area without knowing anything about the angles.

Since this sounds like homework, I'm not certain if this solution is permitted or not by your teacher, but it might be worth investigating.

Heron's formula is defined as

A = sqrt(s(s - a)(s - b)(s - c))

...where a, b, and c are the side lengths and s is defined as:

s = (a + b + c) / 2

I'll leave it as an exercise to you to figure out how to implement the formula (if you decide to use it). As a hint, the Math module has a sqrt method you can use.

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
  • You're a life saver. I needed that so bad, I've been working on this for hours. I knew i was over thinking it but i didn't know how. – Lee__Roberts Sep 10 '14 at 04:10