How to Validate a State or Province (Name or Abbreviation) and Get Location Information About it using Java
When website users enter the name of specific states or provinces into an address field, it’s reasonable to expect they will use abbreviations in the place of full names which come with long & unwieldy spellings. For example, two states in the USA — Connecticut and Massachusetts — are commonly abbreviated to CT and MA respectively (for obvious reasons).
Using our Validate State or Province API, you can confirm the validity of state/province name entries in both abbreviated and full-length form, so long as the full name of the state’s/province’s country (or country code) is also provided. In its response, this API will determine the validity of the state/province argument, and if valid, it will provide locational information (latitude & longitude) about the state/province in question. Further, it will return the full name of the state/province in question, without regard to the original form of entry (i.e., “CT” will be returned as “Connecticut”). For your reference, I’ve included an example JSON response model for the arguments “CT” and “United States.”
The following input parse request:
{
"StateOrProvince": "CT",
"CountryFullName": "United States",
"CountryCode": "string"
}
Returns the following response:
{
"ValidState": true,
"StateOrProvince": "Connecticut",
"Latitude": 41.6500201,
"Longitude": -72.7342163
}
Below, I’ll demonstrate how you can utilize this API & structure your API call with ready-to-run, complementary code examples in Java.
To begin, let’s first install the API client with Maven. We can do so by adding a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, let’s add a reference to the pom.xml dependency, which will allow JitPack to dynamically compile the library:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
With installation complete, we can now include the imports at the top of our file. After that, let’s use the example parse request format above to structure our arguments and pass those through the function below:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.AddressApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");AddressApi apiInstance = new AddressApi();
ValidateStateRequest input = new ValidateStateRequest(); // ValidateStateRequest | Input parse request
try {
ValidateStateResponse result = apiInstance.addressValidateState(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AddressApi#addressValidateState");
e.printStackTrace();
}
Your final step is to authenticate the service with a Cloudmersive API key; you can get one by registering a free account on our website (free accounts come with a hard limit of 800 API calls per month & zero additional commitments). Simply copy & paste your API key string into the appropriate field (indicated in the code comments) above, and you’re all done. Nice & easy!