Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to get an Array of US cities?

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Thanked 0 Times in 0 Posts

    Default How to get an Array of US cities?

    Is there a way in my preferred JAVA IDE (Eclipse/Netbeans) in which i can construct an array of US Cities. I'm creating a Tree in which the user can select a runnable server. So in this case, the user will click on "Servers", then down to "United States", then a whooooolllleeeee bunch of United States cities will appear. I did it with the UK cities with no problem.
    String[] ukCities = {"Bath", "Birmingham", "Bradford", "Brighton & Hove", 
    				"Bristol", "Cambridge", "Canterbury", "Carlisle", "Chelmsford",
    				"Chester", "Chickester", "Coventry", "Derby", "Durham", "Ely", "Exeter",
    				"Gloucester", "Hereford", "Kingston upon Hill", "Lancaster", "Leeds", "Leicester",
    				"Lichfield", "Lincoln", "Liverpool", "City of London", "Manchester", 
    				"Newcastle upon Tyne", "Norwich", "Nottingham", "Oxford", "Peterborough", "Plymouth", 
    				"Preston", "Ripon", "Salford", "Salisbury", "Sheffield", "Southampton", "St Albans",
    				"Stoke-on-Trent", "Sunderland", "Truro", "Wakefield", "Wells", "City of Westminister", 
    				"Wolverhampton", "Worcester", "York"};
    DefaultMutableTreeNode[] ukNode = new DefaultMutableTreeNode[50];
    		for(int i = 0; i < 49; i++) {
    			ukNode[i] = new DefaultMutableTreeNode(new ServerInfo (ukCities[i], i, selectedServers.contains(i)));
    And baaaaammm, it works. I want something similar to this, but it's just waaaaay to time consuming adding over 200+ cities and I really don't want to spend my whole day typing out each individual city from a wikipeda page... Maybe there is something Java already provides for us, and I just don't know what?

    Any help, would be greatly approved of. For now, I will go and make myself a cup of tea and enjoy some rich tea biscuits...

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: How to get an Array of US cities?

    Do you have them in a file somewhere? Just load them from that instead. I'm sure you can find a file online.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    ProgrammablePeter (February 12th, 2014)

  4. #3
    Junior Member
    Join Date
    Dec 2013
    Thanked 0 Times in 0 Posts

    Default Re: How to get an Array of US cities?

    Cheers, I've used that way. Didn't realise I could do it from a text file, added them now. Cheers Kevin!

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Thanked 329 Times in 305 Posts

    Default Re: How to get an Array of US cities?

    Also, when you are trying to populate a file with information like this, try not to do it manually. There are webservices available for things like this. In fact, the SBA (US Small Business Association) has a public one for this exact purpose.
    Here is a link to their webservice documentation: U.S. City and County Web Data API | SBA.gov

    To query the webservice from Java, you would just open a url connection to the desired information and parse it as an XML file or a JSON message. So, if you wanted all the cities in Florida, you would open a connection to:
    You can view that page in your browser to see the result it provides.

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Blue Pelican Java- Array of Hope; char array for loops?
    By Draco579 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 11th, 2017, 05:36 AM
  2. Java 1.4 Array issues - controlling array size for dynamic usage
    By doonan79 in forum Collections and Generics
    Replies: 5
    Last Post: June 18th, 2013, 11:53 AM
  3. compile errors when creating a 2nd array the same size as 1st array
    By javaiscool in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 09:35 PM
  4. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  5. Urgently need help with counting total of cities/fines for an output file!!
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 28th, 2012, 08:44 PM