The program needs to take 3 x values followed by 3 y values to create 3 points that form a triangle, then calculate the area of the triangle. I'm pretty convinced the logic is right, and it should be an easy fix.
import java.util.*; public class ComputeTriangle { public static void main(String[]args) { Scanner input = new Scanner(System.in); System.out.print("Please input the three points for a triangle: "); double x1, x2, x3, y1, y2, y3, side1, side2, side3, s, area, calculate; x1 = input.nextDouble(); x2 = input.nextDouble(); x3 = input.nextDouble(); y1 = input.nextDouble(); y2 = input.nextDouble(); y3 = input.nextDouble(); side1 = (x2-x1)/(y2-y1); side2 = (x3-x1)/(y3-y1); side3 = (x3-x2)/(y3-y2); s = (side1 + side2 + side3)/2; area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3)); System.out.println("The area of the triangle is: " + area); } }
Output:
Please input the three points for a triangle: 1.5 -3.4 4.6 5 9.5 -3.4
The area of the triangle is: NaN
Press any key to continue . . .
-----------
Can anyone help me get rid of this 'Not a Number' runtime error?