Task: Write a program that calculates the mileage and/or cost of fuel by asking the user to input two cities.
It only works for two places and not all three, why?
How can I put just the name of the location instead of inputting the numbers?

package Winterproject;

import java.lang.Math;
import java.lang.String;
import java.util.Scanner;


public class Mileage {


        public static void main(String[] args) {

            Scanner in=new Scanner(System.in);
            String LocationNames;
            int Location1=0;
            int Location2=0;
            int Location3=0;
            double Distance1;
            double lat1 = 0;
            double lat2 = 0;
            double Distance2;
            double lng1 = 0;
            double lng2 = 0;
            double distance1=0;
            double c = 0;
            double distance2=0;
            double Radius = 3961;
            double costoffuel = 3.64;


            System.out.println("Write your starting location.");
            Location1=in.nextInt();
            // System.out.println("Choose a city: enter 1 for Newyork, 2 for California, and 3 for kansas.");
            //City2=in.nextInt();
             System.out.println("Write your destination.");
            Location3=in.nextInt();

            switch(Location1){
                case 1:
                    lat1 =33;
                    lng1 =84; 
                   LocationNames="Atlanta";
                    break;
                case 2:
                    lat2=27;
                    lng2= 122;
                    LocationNames="Seattle";
                    break;
                case 3:
                    lat2=21;
                    lng2= 157;
                    LocationNames="Honolulu";
                    break;
                default:

            }
                    switch(Location2){
                    case 1:
                        lat1 =33;
                        lng1 =84; 
                       LocationNames="Atlanta";
                        break;
                    case 2:
                        lat2=27;
                        lng2= 122;
                        LocationNames="Seattle";
                        break;
                    case 3:
                        lat2=21;
                        lng2= 157;
                        LocationNames="Honolulu";
                        break;
                    default:

              }
                        switch(Location3){
                        case 1:
                            lat1 =33;
                            lng1 =84; 
                           LocationNames="Atlanta";
                            break;
                        case 2:
                            lat2=27;
                            lng2= 122;
                            LocationNames="Seattle";
                            break;
                        case 3:
                            lat2=21;
                            lng2= 157;
                            LocationNames="Honolulu";
                            break;
                        default:
                            System.out.println("Wrong input.");
            }
            Distance2 = (lat2 - lat1) *(lat2 -lat1) ;
            Distance1 = (lng2 - lng1) * (lng2 -lng1);  
            distance1 =(Math.sin(Distance2/2)) +Math.cos(lat2) * Math.cos(lat1) * Math.sin(Distance1/2); 
            c = 2 * Math.atan2(Math.sqrt(distance1),Math.sqrt(1+distance1)) ;
            distance2 = Radius * c/2.03833;
    System.out.println("Distance between the two locations: " + distance2 );
    //System.out.println("howmany");

    double mileage = (distance2/costoffuel); 
    System.out.println("Mileage: "+mileage);
    }
    }

There are lots of ways...
One way is to create 3 arrays for name, lat and long, and populate them with the location data. Then when the user enters a name you can search the name array, then use the index where you found it to get the lat and log from the other arrays. This also gets rid of all those repetative switch statements.
A more Java/O.O. way is to create a small Location class, with name, lat, long as its instance variables. Create instances for each location, and put those instances in an array or ArrayList. Then you can search for the right name, and when you find the matching Location you will have the corresponding lat and long.
Or... create a small class to hold lat/long pairs, and create a Map with name as key and lat/long as value. Then you can use Map's get method to get the lat/long directly from the name.
... will that do for a start?

This article has been dead for over six months. Start a new discussion instead.