The problem
Write a program that reads the contents of the two files into two separate arrays, or ArrayLists. The user should be able to enter a boy’s name, or a girl’s name, or both, and the application will display a message indicating whether the names were among the most popular.
The exercises will use two files:
- GirlNames.txt - this file contains a list of the 200 most popular names given to girls born in the United States for the years 2000 through 2009.
- BoysNames.txt - this file contains a list of the 200 most popular names given to boys born in the United States fro the years 2000 through 2009.
In the code below we will read a text file using java 8 retrieving the names from the two files creating an associated arraylist for girls and boys names. We will then search for the name entered in each of lists using a lambda expression and java 8's stream anyMatch method.
Breaking it down
public static void main(String args[]) throws IOException {
Path boysNamesPath = Paths
.get("src/main/resources/com/levelup/java/exercises/beginner/BoyNames.txt")
.toAbsolutePath();
Path girlsNamePath = Paths
.get("src/main/resources/com/levelup/java/exercises/beginner/GirlNames.txt")
.toAbsolutePath();
// read boys names
List<String> boysNames = Files.lines(boysNamesPath).collect(
Collectors.toList());
// ready girls names
List<String> girlsNames = Files.lines(girlsNamePath).collect(
Collectors.toList());
// ask user get name from user
String searchName = getNamesFromUser();
displaySearchResults(searchName, boysNames, girlsNames);
}
/**
* Method should ask user to input a name
*
* @return name to be searched
*/
public static String getNamesFromUser() {
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
// Get a name from user
System.out.println("Popular Name Search");
System.out.print("Enter a name: ");
String name = keyboard.nextLine();
keyboard.close();
return name;
}
/**
* Method should determine and output if a name is contained with in lists
* passed
*
* @param searchName
* @param boysNames
* @param girlsNames
* @throws IOException
*/
public static void displaySearchResults(String searchName,
List<String> boysNames, List<String> girlsNames) throws IOException {
System.out.println("\nHere are the search results:\n");
boolean popularBoyName = boysNames.stream().anyMatch(
p -> p.equalsIgnoreCase(searchName));
boolean popularGirlName = girlsNames.stream().anyMatch(
p -> p.equalsIgnoreCase(searchName));
// Display the results.
if (popularBoyName) {
System.out.println(searchName + " is a popular boy's name.");
}
if (popularGirlName) {
System.out.println(searchName + " is a popular girl's name.");
}
if (!popularBoyName && !popularGirlName) {
System.out.println(searchName + " is not a popular name.");
}
}
Output
Popular Name Search
Enter a name: Alexander
Here are the search results:
Alexander is a popular boy's name.
Level Up
- Modify the program to to allow users to enter in multiple names seperated by a comma as input. Refernece split a string by a comma.