TAGS :Viewed: 17 - Published at: a few seconds ago

[ How do you get a doubleValue to get an output? ]

I am working on this project for class, but can't seem to get an answer to the equation.

What I want -

Enter the A value for the line: 2.45

Enter the B value for the line: 4

Enter the C value for the line: -8

Enter the x coordinate of the point: 2.17

Enter the y coordinate of the point: -4

Distance from the point to the line is: 3.9831092774319026

What I am getting - Distance from the point to the line is: NaN

import java.io.*;
import java.util.*;
public class DistToline {
public static double A;
public static double B;
public static double C;
public static double distance;

public static double
getDist(double a, double b){
    distance= Math.abs(((A*a)+(B*b)+(C))/(Math.pow(A, 2))+(Math.pow(B, 2)));
    return distance;
 }


 public static void main(String args[])
 {
    Scanner f= new Scanner(System.in);
    System.out.print("Enter the A value for the line:");
    Double A = f.nextDouble();

    Scanner g= new Scanner(System.in);
    System.out.print("Enter the B value for the line:");
    Double B = g.nextDouble();

    Scanner h= new Scanner(System.in);
    System.out.print("Enter the C value for the line:");
    Double C = h.nextDouble();

    Scanner i= new Scanner(System.in);
    System.out.print("Enter the x coordinate of the point:");
    Double X = i.nextDouble();

    Scanner j= new Scanner(System.in);
    System.out.print("Enter the y coordinate of the point:");
    Double Y = j.nextDouble();

    System.out.print("Distance from the point to the line is: ");
    System.out.println(getDist(5,4));

 }
}

From what I think I am doing wrong is that I am not doing the calculations and am not returning the distance as a double is this why I am not getting an output? If so how do I fix it?

Answer 1


Double A = f.nextDouble();

This does not assign the value to the member

public static double A;

it creates a local variable called A.

Change to

A = f.nextDouble();

And repeat for all those other member variables.

Answer 2


For variables A,B,C ,you already declare static and again in main method,you are again declare variables as double thats why value for A & B becomes zero and NaN appears.Below is your running code with minor changes-

public class DistToline {
public static double A;
public static double B;
public static double C;
public static double distance;

public static double
getDist(double a, double b){
    distance= Math.abs(((A*a)+(B*b)+(C))/(Math.pow(A, 2))+(Math.pow(B, 2)));
    return distance;
 }



public static void main(String args[])
{
   Scanner f= new Scanner(System.in);
   System.out.print("Enter the A value for the line:");
    A = f.nextDouble();

   Scanner g= new Scanner(System.in);
   System.out.print("Enter the B value for the line:");
    B = g.nextDouble();

   Scanner h= new Scanner(System.in);
   System.out.print("Enter the C value for the line:");
    C = h.nextDouble();

   Scanner i= new Scanner(System.in);
   System.out.print("Enter the x coordinate of the point:");
   Double X = i.nextDouble();

   Scanner j= new Scanner(System.in);
   System.out.print("Enter the y coordinate of the point:");
   Double Y = j.nextDouble();
   System.out.print("Distance from the point to the line is: ");
   System.out.println(getDist(5,4));

}