-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1666b92
commit d893ea1
Showing
3 changed files
with
496 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
src/main/java/com/levelup/java/exercises/beginner/NameSearch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.levelup.java.exercises.beginner; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* This java exercises will demonstrate how to search for name within a file. | ||
* | ||
* @author Justin Musgrove | ||
* @see <a href='http://www.leveluplunch.com/java/exercises/name-search/'>Name search</a> | ||
*/ | ||
public class NameSearch { | ||
|
||
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."); | ||
} | ||
} | ||
|
||
} |
200 changes: 200 additions & 0 deletions
200
src/main/resources/com/levelup/java/exercises/beginner/BoyNames.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
Jacob | ||
Michael | ||
Joshua | ||
Matthew | ||
Daniel | ||
Christopher | ||
Andrew | ||
Ethan | ||
Joseph | ||
William | ||
Anthony | ||
David | ||
Alexander | ||
Nicholas | ||
Ryan | ||
Tyler | ||
James | ||
John | ||
Jonathan | ||
Noah | ||
Brandon | ||
Christian | ||
Dylan | ||
Samuel | ||
Benjamin | ||
Zachary | ||
Nathan | ||
Logan | ||
Justin | ||
Gabriel | ||
Jose | ||
Austin | ||
Kevin | ||
Elijah | ||
Caleb | ||
Robert | ||
Thomas | ||
Jordan | ||
Cameron | ||
Jack | ||
Hunter | ||
Jackson | ||
Angel | ||
Isaiah | ||
Evan | ||
Isaac | ||
Mason | ||
Luke | ||
Jason | ||
Gavin | ||
Jayden | ||
Aaron | ||
Connor | ||
Aiden | ||
Aidan | ||
Kyle | ||
Juan | ||
Charles | ||
Luis | ||
Adam | ||
Lucas | ||
Brian | ||
Eric | ||
Adrian | ||
Nathaniel | ||
Sean | ||
Alex | ||
Carlos | ||
Bryan | ||
Ian | ||
Owen | ||
Jesus | ||
Landon | ||
Julian | ||
Chase | ||
Cole | ||
Diego | ||
Jeremiah | ||
Steven | ||
Sebastian | ||
Xavier | ||
Timothy | ||
Carter | ||
Wyatt | ||
Brayden | ||
Blake | ||
Hayden | ||
Devin | ||
Cody | ||
Richard | ||
Seth | ||
Dominic | ||
Jaden | ||
Antonio | ||
Miguel | ||
Liam | ||
Patrick | ||
Carson | ||
Jesse | ||
Tristan | ||
Alejandro | ||
Henry | ||
Victor | ||
Trevor | ||
Bryce | ||
Jake | ||
Riley | ||
Colin | ||
Jared | ||
Jeremy | ||
Mark | ||
Caden | ||
Garrett | ||
Parker | ||
Marcus | ||
Vincent | ||
Kaleb | ||
Kaden | ||
Brady | ||
Colton | ||
Kenneth | ||
Joel | ||
Oscar | ||
Josiah | ||
Jorge | ||
Cooper | ||
Ashton | ||
Tanner | ||
Eduardo | ||
Paul | ||
Edward | ||
Ivan | ||
Preston | ||
Maxwell | ||
Alan | ||
Levi | ||
Stephen | ||
Grant | ||
Nicolas | ||
Omar | ||
Dakota | ||
Alexis | ||
George | ||
Collin | ||
Eli | ||
Spencer | ||
Gage | ||
Max | ||
Cristian | ||
Ricardo | ||
Derek | ||
Micah | ||
Brody | ||
Francisco | ||
Nolan | ||
Ayden | ||
Dalton | ||
Shane | ||
Peter | ||
Damian | ||
Jeffrey | ||
Brendan | ||
Travis | ||
Fernando | ||
Peyton | ||
Conner | ||
Andres | ||
Javier | ||
Giovanni | ||
Shawn | ||
Braden | ||
Jonah | ||
Cesar | ||
Bradley | ||
Emmanuel | ||
Manuel | ||
Edgar | ||
Erik | ||
Mario | ||
Edwin | ||
Johnathan | ||
Devon | ||
Erick | ||
Wesley | ||
Oliver | ||
Trenton | ||
Hector | ||
Malachi | ||
Jalen | ||
Raymond | ||
Gregory | ||
Abraham | ||
Elias | ||
Leonardo | ||
Sergio | ||
Donovan | ||
Colby | ||
Marco | ||
Bryson | ||
Martin |
Oops, something went wrong.