Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Area of a triangle (using 2 extra methods) error help

1. ## Area of a triangle (using 2 extra methods) error help

EDITED..

Oke, I managed to sort everything out, except, it does not calculate the area in area() method:

```[COLOR="Red"]public static double area(double side1, double side2, double side3, double areaTri)[/COLOR]
{
double s = (side1 +  side2 + side3)/2;
areaTri = Math.sqrt(s*(s - side1)*(s - side2)*(s - side3));

//Returns to main() and should print out the last statement with the area
return areaTri;
}```

Updated Code:
```//Calculate the area of a triangle

import java.util.Scanner;
import static java.lang.Math.*;

[COLOR="Lime"]public class MyTriangle[/COLOR]
{
[COLOR="Red"]public static void main(String[] args)[/COLOR]
{
double areaTri=0;
Scanner input = new Scanner(System.in);

System.out.print("Enter the size of the first side: ");
double side1 = input.nextDouble();

System.out.print("Enter the size of the second side: ");
double side2 = input.nextDouble();

System.out.print("Enter the size of the third side: ");
double side3 = input.nextDouble();

isValid(side1,side2, side3, areaTri);

System.out.println("");
System.out.println("The area of the triangle is "+ areaTri);
}
//Check if the sum of side1 and side2 is greater than side3 and if not returns false
//If true calls on method area()
[COLOR="Red"]public static boolean isValid(double side1, double side2, double side3, double areaTri)[/COLOR]
{
if(side1 * side2 > side3)
{
area(side1,side2,side3, areaTri);
return true;
}
else
{
System.out.println("The input is invalid!");
return false;
}
}
//calculates area of the triangle assuming conditions are met at method isValid()
[COLOR="red"]public static double area(double side1, double side2, double side3, double areaTri)[/COLOR]
{
double s = (side1 +  side2 + side3)/2;
areaTri = Math.sqrt(s*(s - side1)*(s - side2)*(s - side3));
return areaTri;
}
}```

2. ## Re: Area of a triangle (using 2 extra methods) error help

, it does not calculate the area in area() method:
What is it supposed to do in that method?
Do you have a question?

If you need help debugging your code, I suggest that you separate out all the subexpressions in the sqrt() method call and print them out to see if they are computing to the values you expect.

3. ## Re: Area of a triangle (using 2 extra methods) error help

I managed to fix it in the main():

From:

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

To:

`System.out.println("The area of the triangle is "+ area(side1,side2,side3,areaTri));`

This solved the problem and lead to another. How do I round it off to 1 decimal place. Right now the output would look something like this:

0.7261843774138906

4. ## Re: Area of a triangle (using 2 extra methods) error help

How do I round it off to 1 decimal place
A couple of ways
The printf() has formatting strings
The DecimalFormat class will format a number.

5. ## Re: Area of a triangle (using 2 extra methods) error help

I decided to use printf() since I'm more familiar with it, but it doesnt seem to work the same:

In C this would normally work:
`printf("The area of the triangle is %0.1f", area(side1,side2,side3,areaTot));`

I get a format specifier error in java when running:
`System.out.printf("The area of the triangle is %0.1f", area(side1,side2,side3,areaTot));`

6. ## Re: Area of a triangle (using 2 extra methods) error help

Have you looked up the error message to see what it means? It could tell you what the problem is.

7. ## Re: Area of a triangle (using 2 extra methods) error help

I know what it means...the format specifier "%0.1f" doesnt work, my question is how could I interprate this into java to show a single decimal place.

8. ## Re: Area of a triangle (using 2 extra methods) error help

Try changing it. See the Formatter class for descriptions.

9. ## Re: Area of a triangle (using 2 extra methods) error help

My problem was the 0 in %0.1f. It should look like this: %.1f

Thank you!

10. ## Re: Area of a triangle (using 2 extra methods) error help

You're welcome. Most of it is in the doc.