# Problem with Bearing calculation appearing as NaN

• January 23rd, 2012, 08:57 AM
welshcrossy
Problem with Bearing calculation appearing as NaN
Im getting an NaN error when displaying a calculation. The programme requires the user the enter a bearing and distance from an inital point (this being 0,0). It then findest the closest station and works out the distance. The issue im getting is when it is trying to work out what direction the closest station is situated based on degrees. It coud be something simple that the IF is wrong but i'm getting very confused. Can anyone please help.

import javax.swing.*;

public class Assignment {

public static void main(String[] args) {
double tempBearing;
double tempLength;
double distanceToClosest;
double stationx;
double stationy;
double currentx;
double currenty;
double bearing;
double degree;

//an array of stations
Station[] stations = new Station[16];
//populate the array with stations
stations[0] = new Station("Windmill Hill",new Location(60.0,12.0));
stations[1] = new Station("Trim Bridge",new Location(63.0,6.0));
stations[2] = new Station("Pirac Cresent",new Location(80.0,8.0));
stations[3] = new Station("Easton",new Location(85.0,12.0));
stations[4] = new Station("Parkway",new Location(102.0,9.0));
stations[5] = new Station("Temple Fields",new Location(140.0,7.0));
stations[6] = new Station("St Dennis",new Location(180.0,1.0));
stations[7] = new Station("Moxbridge",new Location(180.0,2.0));
stations[8] = new Station("Shakespeare Court",new Location(192.0,5.0));
stations[9] = new Station("Weston-On-Shore",new Location(232.0,14.0));
stations[10] = new Station("Jacobs Well",new Location(240.0,3.0));
stations[11] = new Station("Central",new Location(243.0,5.0));
stations[12] = new Station("Newbridge",new Location(245.0,11.0));
stations[13] = new Station("Tivoli",new Location(275.0,6.0));
stations[14] = new Station("Clifton Street",new Location(285.0,11.0));
stations[15] = new Station("St Judes Hill",new Location(355.0,4.0));

//an array of Lines
LineName[] Line = new LineName[16];
//populate the array with LineNames
Line[0] = new LineName ("Docks Line");
Line[1] = new LineName ("Docks Line");
Line[2] = new LineName ("Gyratory Line");
Line[3] = new LineName ("Brunel Line");
Line[4] = new LineName ("Brunel Line");
Line[5] = new LineName ("Gyratory Line");
Line[6] = new LineName ("Docks Line");
Line[7] = new LineName ("Brunel Line");
Line[8] = new LineName ("Gyratory Line");
Line[9] = new LineName ("Docks Line");
Line[10] = new LineName ("Docks Line");
Line[11] = new LineName ("Docks Line");
Line[12] = new LineName ("Docks Line");
Line[13] = new LineName ("Brunel Line");
Line[14] = new LineName ("Brunel Line");
Line[15] = new LineName ("Gyratory Line");

//get current location and create a location object

String temp = JOptionPane.showInputDialog("Bearing to current location?");
tempBearing = Double.parseDouble(temp);
temp = JOptionPane.showInputDialog("distance from origin of current?");
tempLength = Double.parseDouble(temp);

Location current = new Location(tempBearing, tempLength);
Station closest = stations[0];
LineName line = Line[0];
distanceToClosest = current.calcDistance(stations[0].location);

for(int i = 1; i < stations.length; i++){
double tempDist = current.calcDistance(stations[i].location);

if(tempDist < distanceToClosest){
closest = stations[i];
line = Line[i];
distanceToClosest = tempDist;
}//if (tempDist < distanceToClosest)
}//for loop

stationx = current.calcx(stations[0].location);
stationy = current.calcy(stations[0].location);
bearing = Math.asin((stationx - currentx ) / distanceToClosest );
bearing = Math.toDegrees(bearing);

if ((stationy >= currenty) && (stationx >= currentx)) { degree = bearing ;
}
else if ((stationy <= currenty) && (stationx >= currentx)) { degree = (180.0 - bearing) ;
}
else if ((stationy <= currenty) && (stationx <= currentx)) { degree = (180.0 + bearing) ;
}
else {
degree = 360.0 - bearing;
}

JOptionPane.showMessageDialog(null, "the closest station is " + closest.name + " on " + line.Lname + "."

+ "\nIt is " + distanceToClosest + " away." + " Bearing of " + degree);

}
}
class Station{ // a station class

Location location;
String name;

public Station(String n, Location l){
name = n;
location = l;
}
}
class LineName{ // a station line class

String Lname;

public LineName(String r){

Lname = r;
}
}
class Location{
double bearing;
double length;

public Location(){
bearing = 0.0;
length = 0.0;
}
public Location(double b, double l){
bearing = b;
length = l;
}
public double calcDistance(Location l){
double angle = Math.abs(bearing - l.bearing);//calculating the angle between the bearings
return Math.pow(temp, 0.5);
}
public double calcx(Location l){
return tempx;
}
public double calcy(Location l){
return tempy;

}

}//location
• January 23rd, 2012, 09:24 AM
newbie
Re: Problem with Bearing calculation appearing as NaN
Prynhawn da Welshcrossy :)

I suggest you read the documentation for Math.asin() here, then put println()'s into your program to see the value returned from computing (stationx - currentx) / distanceToClosest.
• January 23rd, 2012, 10:05 AM
welshcrossy
Re: Problem with Bearing calculation appearing as NaN
Prynhawn da Newbie, thanks for the quick response.

I read that but didnt really make much sense to me. I have put in a bearing of 180 and a length of 40 the (stationx - currentx) / distanceToClosest calulcated 18.20?
• January 23rd, 2012, 10:23 AM
newbie
Re: Problem with Bearing calculation appearing as NaN
The API reads; " If the argument is NaN or its absolute value is greater than 1, then the result is NaN ".
Make sense?