SterlingH
SterlingH

Reputation: 37

If statements not working correctly in program to determine triangle type

I have an assignment to write a program that uses the method triangleType that i must write to take three int inputs from the user and output the triangle type. In the method, I first need to sort the integers in ascending order so that the comparisons I am required to use will work correctly. I know I did the sorting part in the code correctly, because I tested it before I even started trying to determine the triangle type. I am required to use these comparisons to find the triangle type: "if A + B <= C, then the sides do not represent a valid triangle. if A = C (all the sides must be the same length) then the triangle is EQUILATERAL. if A = B or B = C, then the triangle is ISOSCELES; otherwise the triangle is SCALENE" For some reasons my if statements at the end of the triangleType method are not working correctly and I get all sorts of output, including "Invalid Triangle" plus other outputs no matter the integers that I enter.

package trianglemethod;

import javax.swing.JOptionPane;


public class TriangleMethod
{


    public static void main(String[] args)
    {
        String wordaA, wordbB, wordcC, answer;

        do 
        {
           System.out.println("Please enter all 3 side lengths of the triangle in any order.");
           wordaA = JOptionPane.showInputDialog("Enter side 1:");
           wordbB = JOptionPane.showInputDialog("Enter side 2:");
           wordcC = JOptionPane.showInputDialog("Enter side 3:");
           int aA = Integer.parseInt(wordaA);
           int bB = Integer.parseInt(wordbB);
           int cC = Integer.parseInt(wordcC);
           triangleType(aA,bB,cC);
           System.out.println("Would you like to enter another triangle?");
           answer = JOptionPane.showInputDialog("Would you like to enter another triangle?");
        } while (answer.equalsIgnoreCase("yes"));

    }

    static void triangleType(int aA, int bB, int cC) {
        int a=0, b=0, c=0;

        if (aA > bB && aA > cC)
        {
            if (bB > cC)
            {
              a = cC;
              b = bB;
              c = aA;
            }
            else if (cC > bB)
            {
              a = bB;
              b = cC;
              c = aA;
            }
        }
        if (bB > aA && bB > cC)
        {
            if (aA > cC)
            {
              a = cC;
              b = aA;
              c = bB;
            }
            else if (cC > aA)
            {
              a = aA;
              b = cC;
              c = bB;
            }
        }
        if (cC > aA && cC > bB)
        {
            if (aA > bB)
            {
              a = bB;
              b = aA;
              c = cC;
            }
            else if (bB > aA)
            {
              a = aA;
              b = bB;
              c = cC;
            }
        }

        if (a+b<=c)
        {
            JOptionPane.showMessageDialog(null,"Invalid Triangle");
        } if (a==c) {
               JOptionPane.showMessageDialog(null,"Triangle is Equilateral"); 
        } if (a==b || b==c){
                JOptionPane.showMessageDialog(null, "Triangle is Isosceles");
        } 
        else {
            JOptionPane.showMessageDialog(null,"Triangle is Scalene");
        }    
    }
}

Upvotes: 1

Views: 177

Answers (3)

Anthony Yershov
Anthony Yershov

Reputation: 307

There's actually 2 issues.

  1. Let's say a=5, b=5, and c=5. The way your code is working right now it checks:
    if (a==c)
    
    which is true, so it prints "Triangle is Equilateral". Then it checks:
    if (a==b || b==c)
    
    which is also true, so it prints "Triangle is Isosceles".

    In order to prevent it from checking the next if statement once it finds the result you are looking for, all following if statements have to be "else if" statements. The final "else" code will run if all the else if statements above it are false. In your case, it will run as long as your third if statement is false.

  2. Also as Andronicus pointed out, the variables a, b, and c will remain 0 if any side is equal to another as your code only checks for inequalities

    Here is working code for the sorting part:
    if(aA>=bB && aA>=cC){
        a = aA;
        if(bB >= cC){
            b = bB;
            c = cC;
        } else {
            b = cC;
            c = bB;
        }
    } else if (bB >= aA  && bB >= cC){
        a = bB;
        if(aA >= cC){
            b = aA;
            c = cC;
        } else {
            b = cC;
            a = aA;
        }
    } else {
        a = cC;
        if(cC >= aA){
            b = cC;
            c = aA;
        } else {
            b = aA;
            c = cC;
        }
    }
    

Upvotes: 0

chris
chris

Reputation: 86

I'm confused, why can't you just do

        if (a == b && b == c) {
            JOptionPane.showMessageDialog(null,"Triangle is Equilateral");
        } else if ((a == b && a != c) || (a == c && a != b) || (b == c && b != a)) {
            JOptionPane.showMessageDialog(null, "Triangle is Isosceles");
        } else if (a != b && a != c && b != c){
            JOptionPane.showMessageDialog(null,"Triangle is Scalene");
        } else {
            JOptionPane.showMessageDialog(null,"Invalid Triangle");
        }

Upvotes: 0

Mureinik
Mureinik

Reputation: 312289

You have three different if statements there that are evaluated separately. Instead, from the description of the problem, it sounds like you need a single if statement with multiple condition branches (i.e., else if clauses):

if (a+b<=c)
{
    JOptionPane.showMessageDialog(null,"Invalid Triangle");
} else if (a==c) {
       JOptionPane.showMessageDialog(null,"Triangle is Equilateral"); 
} else if (a==b || b==c){
        JOptionPane.showMessageDialog(null, "Triangle is Isosceles");
} else {
    JOptionPane.showMessageDialog(null,"Triangle is Scalene");
}    

Upvotes: 1

Related Questions