Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Getting the location of a user.

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a need to obtain which continent a user is in when they run my application. This can be done fairly easy for someone in, say, North America, as there are only 3 countries, but how can I accomplish this for a European user? Do I have to have a giant if/else chain to check for each country in Europe? This is what I was currently thinking I'd have to do...

Is there some other approach I can use, besides checking the user's country to see what continent the user is on? For a continent like Europe or Asia, this would be almost impossible (or at least take an awfully long time to implement).
Thanks,
Corey
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a way that may work. Load up a Hashtable with the 2 letter country code being the key and a string with the entire country name being the value. Your getCountry line gives you the 2 letter country code, do a "get" on the hashtable with that country code and you'll either get the country or a null (if the code doesn't exit).
Now, you still have to load the countries into the hash table. Look at java.util.Properties class. It's essentially a Hashtable that can be loaded from a property file. You're property file would look something like this:
US=United States of America
CN=Canada
FR=France
and so on.
With this implementation adding or changing country codes wouldn't require any recompile, you'd just change the property file.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I would use a HashMap rather than a long list of if/else ifs - but the basic idea's the same. There doesn't seem to be any way of getting this info directly from Locale or any other standard Java library class that I know of.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I posted the above before I saw Greg's message. But I don't see how that addresses the question of continents. The Properties idea is a good one though - it's simple to adapt to continents. The properties file would be something like:
US = North America
CN = North America
FR = Europe
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are already using JDBC, then you could create a lookup table in the database with a list of countries and their corresponding continents.
This way you can centralize the lookup table if needed for other applications.
Jamie
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic