Results 1 to 3 of 3
  1. #1
    np2392 is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default Help with if else statements

    Hi, I'm new to java. I'm taking a class at my university and my homework is to design a program which will calculate the area of a triangle using Heron's Formula. My instructor also wanted us to use an if else statement so if it is an invalid triangle the program would let the user know.

    I'm not sure what's wrong with my program. I use eclipse, and it tells me "Syntax error on token "else", delete this token"

    Any help is appreciated.

    Java Code:
    import java.util.Scanner;
    
    public class HerosFormula
    {
        public static void main(String[]args)
        {
            System.out.println("Enter the three sides of the triangle to caluclate its area.");
            
            int n1, n2, n3, n4;
            double n5;
            
            Scanner keyboard = new Scanner(System.in);
            n1 = keyboard.nextInt();
            n2 = keyboard.nextInt();
            n3 = keyboard.nextInt();
            
            n4 = (n1 + n2 + n3)/2; 
            
            n5 = Math.sqrt(n4 * (n4-n1) * (n4-n2) * (n4-n3)); 
            
            if ((n1>n2+n3) || (n2>n1+n3) || (n3>n1+n2) || (n1<n2-n3) || (n1<n3-n2) || (n2<n1-n3) || (n2<n3-n1) || (n3<n1-n2) || (n3<n2-n1));
                System.out.println("Invalid triangle");
            else    
                System.out.println("The area of the the triangle is:");
                System.out.println(n5);
            
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default

    You have a ; at the end of the if statement that ends the if statement. The next statements are not part of that if

  3. #3
    np2392 is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

Similar Threads

  1. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 07:02 PM
  2. age: using if statements
    By yasmin k in forum New To Java
    Replies: 2
    Last Post: 10-04-2009, 09:50 PM
  3. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 04:24 PM
  4. How to speed sql Statements?
    By bezudar in forum Advanced Java
    Replies: 3
    Last Post: 11-20-2008, 09:53 AM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 07:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •