Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that prompts the user to enter a year, gender and name, then dis

ID: 3591745 • Letter: W

Question

Write a program that prompts the user to enter a year, gender and name, then displays the ranking of the name for the selected year and gender.

The data is to be stored in two arrays of maps, one for boy’s names and one for girl’s names. Each array must have one element for each of the 10 years of data. Each element in the array is a map (a class from the Java API that implements interface Map) that stores key / value pairs, each pair consisting of a name and its ranking, with the name serving as the key

This what the txt file looks like for year 2000

1 Jacob 30541 Emily 24450

2 Michael 28220 Madison 21771

3 Joshua 25965 Hannah 18802

4 Matthew 25142 Emma 16520

5 Ethan 22099 Alexis 15629

6 Andrew 21996 Ashley 15335

7 Joseph 21872 Abigail 15292

8 Christopher 21665 Sarah 14741

9 Nicholas 21380 Samantha 14652

10 Daniel 21291 Olivia 14627

Explanation / Answer

NameRanking..java

package Testmap;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

public class NameRanking {

//two arrays of maps, one for boy’s names and one for girl’s names.

static ArrayList<Map<String, Integer>> girls = new ArrayList<Map<String, Integer>>();

static ArrayList<Map<String, Integer>> boys = new ArrayList<Map<String, Integer>>();

public static void main(String[] args) {

loadTextDetails();

System.out.println("Enter Details required to get ranking: ");

Scanner input = new Scanner(System.in);

int id = 1;

while (id > 0) {

System.out.println("Enter Name: ");

String name = input.nextLine();

System.out.println("Enter Gender(boy/girl): ");

String gender = input.nextLine();

System.out.println("Enter Year: ");

int year = input.nextInt();

int rank = getRank(name, gender);

if (rank != 0) {

System.out.println("Rank of " + name + " is :" + rank);

System.out.println("Press (1) to continue search for next member or (0) to quit");

} else {

System.out.println("Name not available in the ranking.");

System.out.println("Press (1) to continue search for next member or (0) to quit");

}

id=input.nextInt();

}

input.close();

}

//loads the details from the text and saves to Arrays of girls and boys

public static void loadTextDetails() {

File inputFile = new File("D:\workspace\Test\src\Testmap\details.txt");

Scanner inputStream;

String line;

try {

inputStream = new Scanner(inputFile);

while (inputStream.hasNextLine()) {

line = inputStream.nextLine();

String[] values = line.split(" ");

Map<String, Integer> boy = new HashMap<String, Integer>();

boy.put(values[1], Integer.parseInt(values[2].trim()));

Map<String, Integer> girl = new HashMap<String, Integer>();

girl.put(values[3], Integer.parseInt(values[2].trim()));

girls.add(girl);

boys.add(boy);

}

inputStream.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

//Checks whether the given name exists and retrieves the corresponding rank

public static int getRank(String name, String gender) {

int rank = 0;

if (gender.equalsIgnoreCase("girl")) {

for (Map<String, Integer> girlMap : girls) {

if (girlMap.containsKey(name)) {

rank = girlMap.get(name);

}

}

}

if (gender.equalsIgnoreCase("boy")) {

for (Map<String, Integer> boyMap : boys) {

if (boyMap.containsKey(name)) {

rank = boyMap.get(name);

}

}

}

return rank;

}

}

Sample output:

Enter Details required to get ranking:
Enter Name:
Jacob
Enter Gender(boy/girl):
boy
Enter Year:
2000
Rank of Jacob is :30541
Press (1) to continue search for next member or (0) to quit