pjrey2
pjrey2

Reputation: 5

Not A Number Error even after casting and scanning for nextDouble. (Java) Help please?

I am having trouble when I run this program. The loop and calculations work fine. For some reason, it will only calculate areas under 50. Small numbers run fine, but the larger calculations get a NaN result, which is a problem. Any help?

package Area;

import java.util.; import java.text.;

public class AreaPtTwo {

//this creates a scanner object for the user to input through
final static Scanner sc = new Scanner(System.in);


public static void main (String[] args){
    boolean playAgain = true;
    DecimalFormat fmt = new DecimalFormat("#.###");
    while(playAgain){
        //gets the three inputs from the user
        double a = getNum(1);
        double b = getNum(2);        
        double c = getNum(3);

        // calculates 's' = perimeter/2
        double s = (a+b+c)/2.0;

        //this calculates the area of the triangle by getting the square root of (s(s-a)(s-b)(s-c)) --> this is hard to read but there isn't really a better way to put it in comments.
        double area = (double) Math.sqrt(s*(s-a)*(s-b)*(s-c));

        System.out.println("Area: " + fmt.format(area));

        //Closes 
        System.out.println("_______________________________________________________________________________________________________");
        System.out.println("Play again? (y/n)");
        String again = sc.next();
        if(again.charAt(0) == 'y') playAgain = true;
        else if (again.charAt(0) == 'n') playAgain = false;
        else playAgain = false;
    }
}

// this method is to get the input from the user. It is in a method because it is performed three times. this will only accept numbers, no strings or characers allowed
public static double getNum(int n){
    System.out.println("Enter the length of side " + n +":");

    // this will loop until to make sure that the user does not enter characters or words, and will prompt the user until a number is entered
    while (!sc.hasNextDouble()){
        System.out.println("Invalid input\nEnter the length of side " + n +":");
        sc.next(); // this has to be sc.next() because it will throw a hige error if someone enters a word
    }

    // this returns the next double entered as the variable. 
    return sc.nextDouble();
}

}

Upvotes: 0

Views: 79

Answers (2)

McLovin
McLovin

Reputation: 3674

Mathematically, s(s-a)(s-b)(s-c) should be negative if the sides cannot constitute an actual triangle, and the square root of a negative returns Nan. It appears that when you get a NaN, the problem is in geometry, not programming. For any numbers that can form an actual triangle, it works fine.
If you don't know what I mean, try drawing a triangle with sides of length 1, 2, and 5.

Upvotes: 1

BruceWayne
BruceWayne

Reputation: 299

You're getting a negative value for your Math.sqrt() function, try re-analyzing your s*(s-a)*(s-b)*(s-c) area - any negative values will return NaN

Upvotes: 1

Related Questions