import java.util.Scanner; public class HeronsFormula { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter the length of the three sides of your triangle: "); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); System.out.println("The lengths of the sides of your triangle are: " + '\n' + a + '\n' + b + '\n' + c); System.out.println("The are of your triangle according to Heron's Formula is: " + getArea(a,b,c)); } public static double getArea(double a, double b, double c) { double s = (a + b + c)/2; double x = ((s) * (s-a) * (s-B)/> * (s-c)); double area = Math.sqrt(x); return area; } }

## 5 Replies - 32800 Views - Last Post: 08 October 2012 - 11:22 PM

### #1

# Java code using to calculate the area of a triangle w/Heron's Form

Posted 01 October 2012 - 04:18 PM

I have to use methods to write a program to allow the user to input the 3 lengths of their triangle and then the program must output the area of the triangle using Heron's Formula to calculate the area. I think my code so far is correct because there are no errors but I keep getting the area =0. I think there may be something wrong with my calculation steps. If somebody could help me it would be much appreciated!

##
**Replies To:** Java code using to calculate the area of a triangle w/Heron's Form

### #2

## Re: Java code using to calculate the area of a triangle w/Heron's Form

Posted 01 October 2012 - 04:19 PM

On line 15, that 2 needs to be 2.0

### #3

## Re: Java code using to calculate the area of a triangle w/Heron's Form

Posted 01 October 2012 - 04:27 PM

Thanks! But it's still giving me the output as 0 /:

Thanks! But it's still giving me the output as 0 /:

and that "B" on line 16 is actually a lowercase b...idk how that happened sorry.

Thanks! But it's still giving me the output as 0 /:

and that "B" on line 16 is actually a lowercase b...idk how that happened sorry.

### #4

## Re: Java code using to calculate the area of a triangle w/Heron's Form

Posted 02 October 2012 - 10:46 AM

i just put it in netbeans and it ran without a problem?

### #5

## Re: Java code using to calculate the area of a triangle w/Heron's Form

Posted 02 October 2012 - 10:55 AM

natecat, on 01 October 2012 - 07:19 PM, said:

On line 15, that 2 needs to be 2.0

That is not true. because the results of ( a + b + c ) will be a double value, double / int (or double / double) will result in a double value.

--

Brovahkiin501, first the B issue is a problem with our forums, don't worry about that. However what input are you using for your triangles. When I run your code, nothing breaks:

Enter the length of the three sides of your triangle: 3 4 5 The lengths of the sides of your triangle are: 3.0 4.0 5.0 The are of your triangle according to Heron's Formula is: 6.0

### #6

## Re: Java code using to calculate the area of a triangle w/Heron's Form

Posted 08 October 2012 - 11:22 PM

HI everyone! Thank you so much for your help. I finally figured this out.

Page 1 of 1