Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2014
    Posts
    7
    Rep Power
    0

    Default 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);
    }
    }
    Attached Thumbnails Attached Thumbnails Switch Statement using longitude and latitude to calculate distance-lon-lat.png  

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Switch Statement using longitude and latitude to calculate distance

    doesnt work
    Please explain.

    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.

  3. #3
    Join Date
    Jan 2014
    Posts
    7
    Rep Power
    0

    Default 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.

  4. #4
    Join Date
    Jan 2014
    Posts
    7
    Rep Power
    0

    Default 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.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Join Date
    Jan 2014
    Posts
    7
    Rep Power
    0

    Default 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);
    }
    }
    Comment: thank you for the help
    Last edited by Norm; 01-01-2014 at 07:54 PM. Reason: 'end' changed to / in code tag

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default 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.

  8. #8
    Join Date
    Jan 2014
    Posts
    7
    Rep Power
    0

    Default 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?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default 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.

  10. #10
    Join Date
    Jan 2014
    Posts
    7
    Rep Power
    0

    Default Re: Switch Statement using longitude and latitude to calculate distance

    It gets both because of distancex and distancey

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Switch Statement using longitude and latitude to calculate distance

    It gets both because of distancex and distancey
    Does it get all 4 of these: lat1 and lon1 and lat2 and lon2 BEFORE it uses them to compute 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.

  12. #12
    Join Date
    Jan 2014
    Posts
    7
    Rep Power
    0

    Default 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

  1. Replies: 3
    Last Post: 11-19-2012, 09:12 PM
  2. Convert XL data in to latitude longitude format?
    By suresh.somepalli in forum Android
    Replies: 4
    Last Post: 11-01-2012, 03:26 PM
  3. Replies: 2
    Last Post: 03-26-2012, 05:27 PM
  4. Calculate X and Y given starting angle and distance
    By nidhirastogi in forum Java 2D
    Replies: 5
    Last Post: 08-18-2008, 11:24 PM
  5. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 06:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •