I'm getting a math error, i dont know where or why, keep adding more things to the console to try to debug it but nothing is working.
TEST VALUES
LAX (33 56 35 N 118 24 43 W)
Tahiti (17 33 27 S 149 36 42 W)
The answer should be close to 4000 miles, i'm getting -1167
CODE:
import javax.swing.JOptionPane;
import java.util.Scanner;
public class FlightPlanning {
/**
* Ausi2004
* CS160 Fall 2012
* Project 1: Navigare Necesse Est
*
* Description: This is a program to calculate the distance between two
* cities on the Earth using their latitude and longitude.
*/
public static void main(String[] args) {
final double radiusOfEarth = 3959;
String city1;
String city2;
String dir;
double dd, mm, ss, dirn = 0;
double lat1;
double lat2;
double lon1;
double lon2;
double gcDistance;
String input;
//Welcome
JOptionPane.showMessageDialog(null, "Welcome to the flight planner!");
//Get them veriablez.
input = JOptionPane.showInputDialog(null, "Where is the origin?");
city1 = input;
input = JOptionPane.showInputDialog(null, "Where are you going?");
city2 = input;
//error check
if ((city1 == null) || (city2 == null))
{
JOptionPane.showMessageDialog(null, "You entered and incorrect input, \n have a nice day.");
System.exit(0);
}
//Latitude and Longitude
input = JOptionPane.showInputDialog(null, "Enter the coordinates of " + city1 + ".\nThe required"
+ " format is dd mm ss n/s dd mm ss e/w");
if (input == null)
System.exit(0);
Scanner inp = new Scanner(input.toUpperCase());
dd = inp.nextDouble();
mm = inp.nextDouble();
ss = inp.nextDouble();
dir = inp.next();
if (dir.equals("N") || dir.equals("E"))
dirn = 1;
if (dir.equals("S") || dir.equals("W"))
dirn = -1;
System.out.println(dd);
System.out.println(mm);
System.out.println(ss);
System.out.println(dirn + "\n");
lat1 = (Math.PI * ((dd + mm/60 + ss/3600) /180) * dirn);
dd = inp.nextDouble();
mm = inp.nextDouble();
ss = inp.nextDouble();
dir = inp.next();
if (dir.equals("N") || dir.equals("E"))
dirn = 1;
if (dir.equals("S") || dir.equals("W"))
dirn = -1;
System.out.println(dd);
System.out.println(mm);
System.out.println(ss);
System.out.println(dirn + "\n");
lon1 = (Math.PI * ((dd + mm/60 + ss/3600)/180) * dirn);
input = JOptionPane.showInputDialog(null, "Enter the coordinates of " + city2 + ".\nThe required"
+ " format is dd mm ss N/S dd mm ss E/W");
if (input == null)
System.exit(0);
Scanner inp2 = new Scanner(input.toUpperCase());
dd = inp2.nextDouble();
mm = inp2.nextDouble();
ss = inp2.nextDouble();
dir = inp2.next();
if (dir.equals("N") || dir.equals("E"))
dirn = 1;
if (dir.equals("S") || dir.equals("W"))
dirn = -1;
System.out.println(dd);
System.out.println(mm);
System.out.println(ss);
System.out.println(dirn + "\n");
lat2 = (Math.PI * ((dd + mm/60 + ss/3600) /180) * dirn);
dd = inp2.nextDouble();
mm = inp2.nextDouble();
ss = inp2.nextDouble();
dir = inp2.next();
if (dir.equals("N") || dir.equals("E"))
dirn = 1;
if (dir.equals("S") || dir.equals("W"))
dirn = -1;
System.out.println(dd);
System.out.println(mm);
System.out.println(ss);
System.out.println(dirn + "\n");
lon2 = (Math.PI * ((dd + mm/60 + ss/3600)/180) * dirn);
//Do them maths
gcDistance = (radiusOfEarth * (Math.acos(Math.sin(lat1))) * (Math.sin(lat2))
+ (Math.cos(lat1)) * (Math.cos(lat2)) * (Math.cos(lon1 - lon2)));
//Display the results, babe
System.out.println(gcDistance);
JOptionPane.showMessageDialog(null, "The ''Great Circle'' distance between " + city1 + " and \n" + city2 + " is "
+ gcDistance + ".");
//End of Program
System.exit(0);
}
}
This post was edited by Ausi2004 on Sep 21 2012 12:11pm