Hi guys, i am trying to find the area of a triangle and for some odd reason i can't get it to produce the right numbers.

Here is my code:

import java.util.Scanner;

public class Exercise2_15 {
    // find the area of a triangle
    public static void main (String [] args) {
        double side1 = 0;
        double side2 = 0;
        double side3 = 0;

        Scanner input = new Scanner(System.in);

        //obtain three points for a triangle
        System.out.print("Enter three points for a triangle: ");
        double side1x  = input.nextDouble();
        double side1y  = input.nextDouble();
        double side2x  = input.nextDouble();
        double side2y  = input.nextDouble();
        double side3x  = input.nextDouble();
        double side3y  = input.nextDouble();

        //find length of sides of triangle
        side1 = Math.sqrt(Math.pow((side2x - side1x), 2)+ Math.pow((side2y - side1y), 2));

        side2 = Math.sqrt(Math.pow((side3x - side2x), 2)+ Math.pow((side3y - side2y), 2));

        side3 = Math.sqrt(Math.pow((side1x - side3x), 2)+ Math.pow((side1y - side3y), 2));


        double s = (side1 + side2 + side3) / 2;

        double area = Math.sqrt(s * (s - side1) * (s - side2) * (s-side3)) * 0.5;

        System.out.println("The area of the triangle is " + area);
    }
}

Here are the error logs:

First Difference Occurs at: byte 64, line 1
On Line 1 its column 64
Line 1 Wrong: Enter three points for a triangle: The area of the triangle is 47.17000000000089
Line 1 Right: Enter three points for a triangle: The area of the triangle is 94.34 
You have only a First Line Error

sdiff side by side difference your output versus solution....
Enter three points for a triangle: The area of the triangle i | Enter three points for a triangle: The area of the triangle i




First Difference Occurs at: byte 64, line 1
On Line 1 its column 64
Line 1 Wrong: Enter three points for a triangle: The area of the triangle is 21.000000000000217
Line 1 Right: Enter three points for a triangle: The area of the triangle is 42.00 
You have only a First Line Error

sdiff side by side difference your output versus solution....
Enter three points for a triangle: The area of the triangle i | Enter three points for a triangle: The area of the triangle i




First Difference Occurs at: byte 64, line 1
On Line 1 its column 64
Line 1 Wrong: Enter three points for a triangle: The area of the triangle is 60.225
Line 1 Right: Enter three points for a triangle: The area of the triangle is 120.45 
You have only a First Line Error

sdiff side by side difference your output versus solution....
Enter three points for a triangle: The area of the triangle i | Enter three points for a triangle: The area of the triangle i

So, how do i get my program to produce the right numbers? It seems to me that all the outputs end up getting divided in half...

Okay... i will admit, i am pretty stupid for not catching the mistake.

Here is the proper code for it:

import java.text.DecimalFormat;
import java.util.Scanner;

public class Exercise2_15 {
    // find the area of a triangle
    public static void main (String [] args) {
        double side1 = 0;
        double side2 = 0;
        double side3 = 0;

        Scanner input = new Scanner(System.in);

        //obtain three points for a triangle
        System.out.print("Enter three points for a triangle: ");
        double side1x  = input.nextDouble();
        double side1y  = input.nextDouble();
        double side2x  = input.nextDouble();
        double side2y  = input.nextDouble();
        double side3x  = input.nextDouble();
        double side3y  = input.nextDouble();

        //find length of sides of triangle
        side1 = Math.sqrt(Math.pow((side2x - side1x), 2)+ Math.pow((side2y - side1y), 2));

        side2 = Math.sqrt(Math.pow((side3x - side2x), 2)+ Math.pow((side3y - side2y), 2));

        side3 = Math.sqrt(Math.pow((side1x - side3x), 2)+ Math.pow((side1y - side3y), 2));


        double s = (side1 + side2 + side3) / 2;

        double area = Math.sqrt(s * (s - side1) * (s - side2) * (s-side3));

        DecimalFormat myFormatter = new DecimalFormat("#,##0.00");

        System.out.println("The area of the triangle is " + myFormatter.format(area));
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.