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

# Thread: Problem with Bearing calculation appearing as NaN

1. ## 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;
//populate the array with stations
stations = new Station("Windmill Hill",new Location(60.0,12.0));
stations = new Station("Trim Bridge",new Location(63.0,6.0));
stations = new Station("Pirac Cresent",new Location(80.0,8.0));
stations = new Station("Easton",new Location(85.0,12.0));
stations = new Station("Parkway",new Location(102.0,9.0));
stations = new Station("Temple Fields",new Location(140.0,7.0));
stations = new Station("St Dennis",new Location(180.0,1.0));
stations = new Station("Moxbridge",new Location(180.0,2.0));
stations = new Station("Shakespeare Court",new Location(192.0,5.0));
stations = new Station("Weston-On-Shore",new Location(232.0,14.0));
stations = new Station("Jacobs Well",new Location(240.0,3.0));
stations = new Station("Central",new Location(243.0,5.0));
stations = new Station("Newbridge",new Location(245.0,11.0));
stations = new Station("Tivoli",new Location(275.0,6.0));
stations = new Station("Clifton Street",new Location(285.0,11.0));
stations = new Station("St Judes Hill",new Location(355.0,4.0));

//an array of Lines
LineName[] Line = new LineName;
//populate the array with LineNames
Line = new LineName ("Docks Line");
Line = new LineName ("Docks Line");
Line = new LineName ("Gyratory Line");
Line = new LineName ("Brunel Line");
Line = new LineName ("Brunel Line");
Line = new LineName ("Gyratory Line");
Line = new LineName ("Docks Line");
Line = new LineName ("Brunel Line");
Line = new LineName ("Gyratory Line");
Line = new LineName ("Docks Line");
Line = new LineName ("Docks Line");
Line = new LineName ("Docks Line");
Line = new LineName ("Docks Line");
Line = new LineName ("Brunel Line");
Line = new LineName ("Brunel Line");
Line = 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;
LineName line = Line;
distanceToClosest = current.calcDistance(stations.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.location);
stationy = current.calcy(stations.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  Reply With Quote

3. ## 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.  Reply With Quote

4. ## 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?  Reply With Quote

5. ## 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?  Reply With Quote