1

so my assignment is to create an object named city and have the longitude, latitude and name of the city and then ask for the number of cities, then the users input of the name, longitude, and latitude into an array for access. How would i accomplish this with what i have now?

import java.util.Scanner;

public class Assignment3 {

    public static void main(String[] args) {

        Scanner lat = new Scanner(System.in);  //for double data
        Scanner lon = new Scanner(System.in);  //for double data
        Scanner city = new Scanner(System.in); //for string data
        Scanner in = new Scanner(System.in);  //for int

        System.out.print("How many cities? ");
        int number = in.nextInt();


        City [] cities = new City [number];

        for(int i = 1; i <= number; i++){

            System.out.println("City # " + i);
            System.out.print("Enter name: " );
            String name = city.Next();
            System.out.print("Enter longitude: ");
            double longitude = lon.nextDouble();
            System.out.println("Enter latitude: ");
            double latitude = lat.nextDouble();

        }

    }

}


public class City {

    String name;      //name of cities
    double lon, lat;  //longitude & latitude

    City(String name, double lon, double lat) 
    {
        this.name = name;
        this.lon = lon;
        this.lat = lat;
    }




    public void report(){    //should report current position of a city

        System.out.println("City: " + this.name); // displays city name
        System.out.println("Longitude: " + this.lon);  //displays longitude
        System.out.println("Latitude: " + this.lat);    //displays latitude
    }

    public double distanceFrom(City otherCity){  //should calculate the distance
        return 0;
    }
}

The point is so that you can then ask for the cities direct location and then the distance between them. Any help is greatly appreciated thank you

Rushery
  • 15
  • 6
  • Btw, you do not need four instances of `Scanner`. And `for(int i = 1; i <= number; i++)` should be `for(int i = 0; i < number; i++)` since indexing starts with `0`. – pzaenger Jul 04 '17 at 21:12
  • Why are you using so many `Scanner`? One is enough. – Marco Luzzara Jul 04 '17 at 21:13
  • Is there a specific issue you are having? – OldProgrammer Jul 04 '17 at 21:14
  • 1
    @OldProgrammer yes, my problem is putting the information from the user into a single array using the (string, double, double) data types for it so would putting this fix that problem? cities[i] = new City(name,longitude,latitude); } – Rushery Jul 04 '17 at 21:22
  • Welcome to Stack Overflow! Please [re-take the tour](http://stackoverflow.com/tour) to see how the site works and what questions are on topic here, and edit your question accordingly. See also: [Why is "Can someone help me?" not an actual question?](http://meta.stackoverflow.com/q/284236) – Joe C Jul 04 '17 at 22:03
  • @Rushery check my solution for your problem. – Oghli Jul 05 '17 at 08:13

2 Answers2

1

You can achieve that in this simple approach:

Define setter instance methods of City class to each attribute that you want user to input it then in main program iterate over each object of cities and set its Info:

public static void main(String[] args) {

        Scanner input = new Scanner(System.in); 
        System.out.print("How many cities? ");
        int number = input.nextInt();
        City [] cities = new City [number];
         for(int i = 0; i < number; i++){
            cities[i] = new City(); // create new object of City
            System.out.println("City # " + (i+1));
            System.out.print("Enter name: " );
            String name = input.next();
            cities[i].setName(name);
            System.out.print("Enter longitude: ");
            double longitude = input.nextDouble();
            cities[i].setLongitude(longitude);
            System.out.print("Enter latitude: ");
            double latitude = input.nextDouble();
            cities[i].setLatitude(latitude);
        }
}

public class City {

    private String name;      //name of cities
    private double lon, lat;  //longitude & latitude

    City(){  // default constructor
       name="";
       this.lon = 0;
       this.lat = 0;
    }

    City(String name, double lon, double lat) 
    {
        this.name = name;
        this.lon = lon;
        this.lat = lat;
    } 

    public void setName(String n){
         name = n;
    }
    public void setLongitude(double longitude){
         lon = longitude;
    }
    public void setLatitude(double latitude){
         lat = latitude;
    }
}  
Oghli
  • 2,200
  • 1
  • 15
  • 37
0

Not sure if I understood your question but you need to create a city object with the current information you've collected in the for loop. Then save it in your cities array.

for(int i = 0; i < number; i++){

    System.out.println("City # " + (i+1));
    System.out.print("Enter name: " );
    String name = city.Next();
    System.out.print("Enter longitude: ");
    double longitude = lon.nextDouble();
    System.out.println("Enter latitude: ");
    double latitude = lat.nextDouble();

    City a_city = new City(name,longitude,latitude);
    cities[i] = a_city;

}

I changed your for loop from i=1 to i=0

To find the distance between two cities you might want to look at this post Calculate distance between two latitude-longitude points? (Haversine formula)

MalcolmInTheCenter
  • 1,376
  • 6
  • 27
  • 47