Skip to content

Instantly share code, notes, and snippets.

@MichaelLeonffu
Created September 21, 2018 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelLeonffu/da7fd13f6aa271a3fa72c1c64fe56b71 to your computer and use it in GitHub Desktop.
Save MichaelLeonffu/da7fd13f6aa271a3fa72c1c64fe56b71 to your computer and use it in GitHub Desktop.
CS 112 (JAVA) MiraCosta; Absolute Java 6th: Chapter 10: Project 1: NameIndex. HashMap, Scanner, File IO Streams.
/**
*
* Chapter 10; Project 1: NameIndex
*
* @author Michael Leonffu
* @version 20-09-2018
*
* This program interperts two data files: girlnames.txt and boynames.txt which consist of names and their frequncy
* e.g girlsnames.txt
* Jill 999
* Anna 180
* Brooke 314
*
* User interface
* Program prompts user for a name to search the names files
* user can input names with precedding and following whitespace
* user can input names in lowercase or any mixof cases
* user can input more than one valid "token" per line
* After input is recived program will output information on given name
* per girlnames.txt and boynames.txt
* if name is found in list
* user is told the ranking and the frequncy of the given name
* otherwise
* user is told that the name doesn't appear in the list and is given the length of the list
* User can then input another name into the program indenfinatly
*
* Condition: Names in files are expect to be in order from most frequnt to least; Files must also be in working directory
*
* Method:
* Files are inputted into the program via I/O:
* As inputting via Scanner; name, ranking (index/order), and frequncy are added to the respective HashMap varibles
* Use of HashMap for faster search time
* Helper Class:
* Index:
* contains an int pair: ranking and frequency; object used for storing pair information per name
* Helper methods:
* inputFormatting and capitalize:
* together, cleans user input by removing white space, using first valid token, and capitalziing first letter only
*
*/
//file io and user input
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.util.Scanner;
//hashmaps
import java.util.HashMap;
public class NameIndex{
//small attempts to clean the input; remove white space, use first token, captializing first lettter
public static String inputFormatting(String s){
return capitalize(s.trim().split(" ")[0].toLowerCase());
}
//captialize first letter
public static String capitalize(String s){
return s.length() < 1? s : s.substring(0,1).toUpperCase()+s.substring(1,s.length());
}
private class Index{
public int ranking;
public int frequency;
public Index(int ranking, int frequency){
this.ranking = ranking;
this.frequency = frequency;
}
}
public static void main(String[] args){
//conts
String girlFileName = "girlnames.txt";
String boyFileName = "boynames.txt";
//hashmaps
HashMap<String, NameIndex.Index> girlsList = new HashMap<String, NameIndex.Index>();
HashMap<String, NameIndex.Index> boysList = new HashMap<String, NameIndex.Index>();
//adding inputfile from girl and boy
Scanner inputStream = null;
try{
//girls
inputStream = new Scanner(new FileInputStream(girlFileName));
for(int i = 1; inputStream.hasNext(); i++)
girlsList.put(inputStream.next(), new NameIndex().new Index(i, inputStream.nextInt()));
//boys
inputStream = new Scanner(new FileInputStream(boyFileName));
for(int i = 1; inputStream.hasNext(); i++)
boysList.put(inputStream.next(), new NameIndex().new Index(i, inputStream.nextInt()));
}catch(FileNotFoundException e){
System.out.println("File not found in local directory: " + girlFileName + " and/or " + boyFileName);
System.exit(0);
}catch(Exception e){
System.out.println("Unexpected Exception: " + e.getMessage());
System.exit(0);
}
//done with input
inputStream.close();
//user input
Scanner input = new Scanner(System.in);
System.out.println("This program can tell you the ranking and frequency of a name for girls and boys from the files: " + girlFileName + " and " + boyFileName);
while(true){
//gather name information from user
System.out.println("\nEnter a name to search naming ranking index: ");
String name = NameIndex.inputFormatting(input.nextLine()); //that way only one search per input
//check lists for name
if(girlsList.containsKey(name))
System.out.printf("%s is ranked %d in popularity among girls with %d namings.%n", name, girlsList.get(name).ranking, girlsList.get(name).frequency);
else
System.out.printf("%s is not ranked among the top %d girl names.%n", name, girlsList.size());
if(boysList.containsKey(name))
System.out.printf("%s is ranked %d in popularity among boys with %d namings.%n", name, boysList.get(name).ranking, boysList.get(name).frequency);
else
System.out.printf("%s is not ranked among the top %d boy names.%n", name, boysList.size());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment