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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Math.sqrt causing general havoc

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Math.sqrt causing general havoc

    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?


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Math.sqrt causing general havoc

    Hello Garrett93!
    The javadoc says that "If the argument is NaN or less than zero, the result is NaN." You are probably passing the method a less than zero argument. You can check that out by printing every variable you have.
    Also I am not sure about your logic. The sides should be the euclidean distance between points. And the formula for computing the euclidean distance is;
    Point 1 at (x1, y1) and Point 2 at (x2, y2).
    xd = x2 - x1
    yd = y2 - y1;
    Distance = Square Root (xd ^2 + yd ^2)
    Hope it helps.

Similar Threads

  1. [SOLVED] What's causing this Null Pointer Exception?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 29
    Last Post: July 2nd, 2011, 10:34 PM
  2. Cannot find where extra brace is at causing my errors
    By eagle09 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: June 27th, 2011, 07:30 PM
  3. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM
  4. Persistence causing problems with JButton 2D Array
    By easyp in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 21st, 2010, 12:21 PM
  5. setEnabled causing checkbox to deselect
    By dewboy3d in forum AWT / Java Swing
    Replies: 3
    Last Post: May 21st, 2009, 11:36 AM