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

Page 1 of 1

## 5 Replies - 27464 Views - Last Post: 08 October 2012 - 11:22 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=293893&amp;s=4344a6f831008ed3148c5fca9bb53619&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Brovahkiin501

Reputation: 0
• Posts: 48
• Joined: 01-October 12

# 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!

```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;
}
}

```

Is This A Good Question/Topic? 0

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

### #2 natecat

Reputation: 56
• Posts: 233
• Joined: 19-December 11

## 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 Brovahkiin501

Reputation: 0
• Posts: 48
• Joined: 01-October 12

## 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.

### #4 kellzor

Reputation: 0
• Posts: 18
• Joined: 01-May 12

## 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 SwiftStriker00

• No idea why my code works

Reputation: 438
• Posts: 1,611
• Joined: 25-December 08

## 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 Brovahkiin501

Reputation: 0
• Posts: 48
• Joined: 01-October 12

## 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.