Find Gender for given name in Java

Hello everyone, In this example I have attached Simple program about, How to Find Gender for given name in Java. This example was done using Third Party API called Gender-API, Which is used to find the gender for given name. For more details please visit official site https://gender-api.com/

Project Structure

Find Gender for given name in Java

GenderFinder.java

package com.javatraineronline;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import com.google.gson.Gson;
public class GenderFinder {
// Service URL
private static final String SERVICE_URL = "https://gender-api.com/get?";
// Your Secret Key provided by Service Provider
private static final String SECRET_KEY = "Your Secret Key";
public static Result getGenderType(String name) {
Result result = null;
// Gson API for JSON to Object Conversion in Java
Gson gson = new Gson();
if(name != null && !name.isEmpty()) {
try {
// Preparing Request URL
final String requestURL = SERVICE_URL + "name="+name + "&key="+ SECRET_KEY;
URL url = new URL(requestURL);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
// Getting the InputStream from URL
InputStream inputStream =  httpURLConnection.getInputStream();
String response = "";
// Reading the Response
if(inputStream != null) {
int data = inputStream.read();
while(data != -1) {
response = response + (char)data;
data = inputStream.read();
}
}
// Closing the Resource
inputStream.close();
System.out.println("Response from (Gender-API Service) :  "+response);
// Converting JSON to Result Object
result = gson.fromJson(response, Result.class);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the name to find gender : ");
String name = scanner.next();
Result result = getGenderType(name);
if(result != null) {
System.out.println("----------------------------");
System.out.println(result.getName()+" is a "+result.getGender());
} else {
System.out.println("No response from service...");
}
scanner.close();
}
}

Result.java

package com.javatraineronline;
public class Result {
private String name;
private String gender;
private String samples;
private String accuracy;
private String duration;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getSamples() {
return samples;
}
public void setSamples(String samples) {
this.samples = samples;
}
public String getAccuracy() {
return accuracy;
}
public void setAccuracy(String accuracy) {
this.accuracy = accuracy;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
}

Output

—————

Enter the name to find gender :

Dinesh

Response from (Gender-API Service) :

{“name”:”dinesh”,”gender”:”male”,”samples”:16255,”accuracy”:100,”duration”:”11ms”}

—————————-

dinesh is a male

Download Source Code

Download Here

References

1. Gender-API Official Site
2. Gender-API Docs

Tags:

4 Responses

  1. Hey It’s really really superb code keep sharing this type of code so that we can learn new things…
    Thanks you so much for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *