Results 1 to 12 of 12
 01012014, 05:47 PM #1Member
 Join Date
 Jan 2014
 Posts
 7
 Rep Power
 0
Switch Statement using longitude and latitude to calculate distance
i got the math part of it it working but when i add the switch statement to give the user options i doesnt work. can anyone point out my problem and a way to fix it?
package homeWork;
import java.lang.Math;
import java.util.Scanner;
import java.lang.String;
public class hw {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
double distancex;
double lat1 = 4;
double lat2 = 5;
double distancey;
double lon1 = 6;
double lon2 = 7;
double distance=0;
double c = 0;
double distance2 = 0;
double Radius = 3961;
double gas = 4.00;
String CityName;
int sp;
int ep;
int City=0;
System.out.println("Choose a city: enter 1 for Newyork, 2 for California, and 3 for Arkansas.");
sp = in.nextInt();
System.out.println("Choose a city: enter 1 for Newyork, 2 for California, and 3 for Arkansas.");
ep= in.nextInt();
switch(City){
case 1:
lat1 =40.6700;
lon1 =73.9400;
CityName="NewYork";
break;
case 2:
lat2=37.0000;
lon2= 120.0000;
CityName="California";
break;
default:
System.out.println("Wrong input.");
}
distancey = (lat2  lat1) *(lat2 lat1) ;
distancex = (lon2  lon1) * (lon2 lon1);
distance =(Math.sin(distancey/2)) +Math.cos(lat2) * Math.cos(lat1) * Math.sin(distancex/2);
c = 2 * Math.atan2(Math.sqrt(distance),Math.sqrt(1+distanc e)) ;
distance2 = Radius * c/2.01582;
System.out.println("The Miles between the two points is " + distance2 + " .");
double mileage = (distance2/gas);
System.out.println("Your mileage is "+mileage);
}
}
 01012014, 06:48 PM #2
Re: Switch Statement using longitude and latitude to calculate distance
doesnt work
Please edit your post and wrap your code with code tags:
[code]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.If you don't understand my response, don't ignore it, ask a question.
 01012014, 06:56 PM #3Member
 Join Date
 Jan 2014
 Posts
 7
 Rep Power
 0
Re: Switch Statement using longitude and latitude to calculate distance
yeah, sorry, new to the site, and the math aspect of my code works but when i incorporated the switch statement part of it the longitude and latitudes wont get read and i would end up with a answer "0.0" . i know the math works for calculating the distance, my problem is the switch statement not getting read properly.
 01012014, 07:05 PM #4Member
 Join Date
 Jan 2014
 Posts
 7
 Rep Power
 0
Re: Switch Statement using longitude and latitude to calculate distance
also i see were there might be confusion i copied the code from when i was testing the math, lat1, lat2,lon1,lon2 arent suppose to have a number since that's what is plugged in when the user makes his choice.
 01012014, 07:21 PM #5Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,655
 Rep Power
 9
Re: Switch Statement using longitude and latitude to calculate distance
You need to show the code you are talking about (and no images please). The code above shows City as 0 and not changing.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 01012014, 07:43 PM #6Member
 Join Date
 Jan 2014
 Posts
 7
 Rep Power
 0
Re: Switch Statement using longitude and latitude to calculate distance
Comment: i was told that by another person but i don't quit understand what it means or how to change it.
Java Code:package homeWork; import java.lang.Math; import java.util.Scanner; import java.lang.String; public class hw { public static void main(String[] args) { Scanner in=new Scanner(System.in); double distancex; double lat1 = 0; double lat2 = 0; double distancey; double lon1 = 0; double lon2 = 0; double distance=0; double c = 0; double distance2 = 0; double Radius = 3961; double gas = 4.00; String CityName; int sp;/*starting place*/ int ep;/*endplace*/ int City=0; System.out.println("Choose a city: enter 1 for Newyork, 2 for California, and 3 for Arkansas."); sp = in.nextInt(); System.out.println("Choose a city: enter 1 for Newyork, 2 for California, and 3 for Arkansas."); ep= in.nextInt(); switch(City){ case 1: lat1 =40.6700; lon1 =73.9400; CityName="NewYork"; break; case 2: lat2=37.0000; lon2= 120.0000; CityName="California"; break; default: System.out.println("Wrong input."); } distancey = (lat2  lat1) *(lat2 lat1) ; distancex = (lon2  lon1) * (lon2 lon1); distance =(Math.sin(distancey/2)) +Math.cos(lat2) * Math.cos(lat1) * Math.sin(distancex/2); c = 2 * Math.atan2(Math.sqrt(distance),Math.sqrt(1+distance)) ; distance2 = Radius * c/2.01582; System.out.println("The Miles between the two points is " + distance2 + " ."); double mileage = (distance2/gas); System.out.println("Your mileage is "+mileage); } }
Last edited by Norm; 01012014 at 07:54 PM. Reason: 'end' changed to / in code tag
 01012014, 07:57 PM #7
Re: Switch Statement using longitude and latitude to calculate distance
The printed message in the default: case needs to show the value of the switch variable so the user can see what the computer sees.
How are the two user inputs used to select the lat/long of the two cities BEFORE the distance between them is computed?If you don't understand my response, don't ignore it, ask a question.
 01012014, 08:05 PM #8Member
 Join Date
 Jan 2014
 Posts
 7
 Rep Power
 0
Re: Switch Statement using longitude and latitude to calculate distance
i dont want them to choose the lat and lon of the city i provide that for them, they just need to choose which two places they want to know the distance between them, but city needs to be given a value?
 01012014, 08:28 PM #9
Re: Switch Statement using longitude and latitude to calculate distance
How does the program get all the values it needs to compute the distance?
It looks like it only gets one set of lat/long BEFORE it does the computations instead of two sets.If you don't understand my response, don't ignore it, ask a question.
 01012014, 08:36 PM #10Member
 Join Date
 Jan 2014
 Posts
 7
 Rep Power
 0
Re: Switch Statement using longitude and latitude to calculate distance
It gets both because of distancex and distancey
 01012014, 08:41 PM #11
Re: Switch Statement using longitude and latitude to calculate distance
It gets both because of distancex and distancey
Add a println statement just before the statement that computes distancex that prints all 4 of those variables' values so you can see if they have valid values.If you don't understand my response, don't ignore it, ask a question.
 01012014, 09:24 PM #12Member
 Join Date
 Jan 2014
 Posts
 7
 Rep Power
 0
Re: Switch Statement using longitude and latitude to calculate distance
Comment: When i first tried that i realized that the variables were getting through the switch statement, so i looked and the start of the code. since i never assigned anything to "sp,ep" and i never used them again after that statement i just replaced them with the variables that do change; "lat1,lon1,lat2,lon2." once i did that the lat and lon of the two codes read and i got the correct answer.
[code]]
package homeWork;
import java.lang.Math;
import java.util.Scanner;
import java.lang.String;
public class hw {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
double distancex;
double lat1 = 0;
double lat2 = 0;
double distancey;
double lon1 = 0;
double lon2 = 0;
double distance=0;
double c = 0;
double distance2 = 0;
double Radius = 3961;
double gas = 4.00;
String CityName;
int City=1000000;
System.out.println("Choose a city: enter 1 for Newyork, 2 for California, and 3 for Arkansas.");
lon1 = in.nextInt();
lat1 =in.nextInt();
System.out.println("Choose a city: enter 1 for Newyork, 2 for California, and 3 for Arkansas.");
lon2= in.nextInt();
lat2 = in.nextInt();
switch(City){
case 1:
lat1 =40.6700;
lon1 =73.9400;
CityName="NewYork";
break;
case 2:
lat2=37.0000;
lon2= 120.0000;
CityName="California";
break;
default:
System.out.println("Wrong input.");
}
distancey = (lat2  lat1) *(lat2 lat1) ;
distancex = (lon2  lon1) * (lon2 lon1);
distance =(Math.sin(distancey/2)) +Math.cos(lat2) * Math.cos(lat1) * Math.sin(distancex/2);
c = 2 * Math.atan2(Math.sqrt(distance),Math.sqrt(1+distanc e)) ;
distance2 = Radius * c/1.56;
System.out.println("The Miles between the two points is " + distance2 + " .");
double mileage = (distance2/gas);
System.out.println("Your mileage is "+mileage);
}
}
[end code]
Thank you for the help i appreciate it, dont think i would have though of that if i was told to check if the values were valid.
Similar Threads

I want to convert this IfElse Statement program to Switch Statement
By UmairBaloch in forum New To JavaReplies: 3Last Post: 11192012, 09:12 PM 
Convert XL data in to latitude longitude format?
By suresh.somepalli in forum AndroidReplies: 4Last Post: 11012012, 03:26 PM 
the switch statement and unreachable statement error
By name in forum New To JavaReplies: 2Last Post: 03262012, 04:27 PM 
Calculate X and Y given starting angle and distance
By nidhirastogi in forum Java 2DReplies: 5Last Post: 08182008, 10:24 PM 
Switch Statement Help
By bluegreen7hi in forum New To JavaReplies: 6Last Post: 02062008, 06:16 AM
Bookmarks