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.

Help with programming homework

I have this program that I need help with. Basically its a program where a user is prompted to enter the length of all three sides of a triangle and the program calculates the area by herons formula and can tell if the triangle is equilateral or Pythagorean. I am having trouble entering a formula to where all three enter sides cant possibly be a triangle. Here is my Program. I need help where the '?' is stated.

Scanner input = new Scanner(System.in);
System.out.println("Welcome to the Triangle Calculation Program");
System.out.print ("Enter length of side one => ");
a = input.nextDouble();
System.out.print ("Enter length of side two => ");
b = input.nextDouble();
System.out.print ("Enter length of side three => ");
c = input.nextDouble();
System.out.println("Your triangle has an area of " + getArea(a,b,c));

Re: Help with programming homework

Where the "?" is, I need to input a formula to where earlier in the program, if I enter 3 sides that cant possibly make a triangle, the 3 inputs will be put in that formula and it will print "Sorry- not a valid triangle". For an example if ( s*(s-a)*(s-b)*(s-c), which is double x, is negative, a triangle cant be formed and will say "Sorry- not a valid triangle". But how would I input that above formula negatively for it to compile and work with the program?

Re: Help with programming homework

It states that variable s in the formula might not have been initialized. But I thought I did that?

--- Update ---

Nevermind, I fixed it. It works now but another problem has come up.

if ( ( s * s-a * s-b * s-c < 0 ) ) {
System.out.println("Sorry- not a valid triangle");
}
It works, but I do not get the message "Sorry- not a valid triangle". I get "Your Triangle has an area of NaN". How does that show up if thats not in my program and how do I get rid of it to say "Sorry- not a valid triangle"?

Re: Help with programming homework

Nevermind, I fixed it. It works now but another problem has come up.

if ( ( s * s-a * s-b * s-c < 0 ) ) {
System.out.println("Sorry- not a valid triangle");
}
It works, but I do not get the message "Sorry- not a valid triangle". I get "Your Triangle has an area of NaN". How does that show up if thats not in my program and how do I get rid of it to say "Sorry- not a valid triangle"?

Re: Help with programming homework

This is what the console window says:
Welcome to the Triangle Calculation Program!
Enter length of side one => 2
Enter length of side two => 5
Enter length of side three => 9
Your Triangle has an area of NaN

Obviously 2,5,9 doesnt make a triangle. And Its not suppose to say "Your triangle has an area of NaN" Its suppose to say "Sorry- not a valid triangle"

Re: Help with programming homework

The area is correct, I transferred the formula straight from the handout for my homework. I dont need it to print the area, if its not a triangle, I cant have an area of the inputs. I just need it to say "Sorry-not a valid triangle". Instead it tries to tell me an area of a triangle that isnt a triangle with a formula that is right.