I am trying to create a Collection class to deal with getting the data of (the amount of male and female names used from the past 130 years(all separate files)) loaded. I know that I need to implement a read(String filename) method in Collection that reads the given file, looks through the existing Names in the file to see if a record with that name exists. I will eventually do more but I would like to start with this.
The following is the code that I made to read a .txt file
public void readInNames() throws IOException
{
// Defines a Scanner to read from an input file.
// This file should be in the same directory
// as the source code for Name.java
File namesFile = new File("yob1880.txt"); // declare the file
// print the directory where this program expects to find names file
System.out.println(System.getProperty("user.dir"));
// ensure file exists and is in the correct directory
if( !namesFile.exists()) {
System.out.println("*** Error *** \n" +
"The names file has the wrong name or is " +
"in the wrong directory. \n" +
"Aborting program...\n\n");
System.exit( -1); // Terminate the program
}
Scanner inputFile = new Scanner( namesFile);
// while( inputFile.hasNext()) {
// names.add( inputFile.nextLine() );
// }
}//end
// Allow looking up a word in names, returning a value of true or false
public boolean nameExists( String namesToLookup)
{
int counter=0;
if( names.contains( namesToLookup)) {
return true; // words was found in names
}
else {
return false; // word was not found in names
}
}//end nameExists
This is where the user picks an option:
System.out.println("Choose from the following options:");
System.out.println(" 1. Search the Names for a string and best year ");
System.out.println(" 2. Search for a name and produce a table of results");
System.out.println(" 3. Choose a year and find the n most popular names from that year ");
System.out.println(" 4. Exit ");
System.out.print("Your choice: ");
String menuChoice = keyboard.nextLine();
This menu choice is where I want to start
if( menuChoice.equals("1") ) {
// Selected "1" to search text
searchText();
// skip rest of code
System.out.println("\n" +
"Exiting...");
System.exit( 0);
}
public void searchText()
{
System.out.print("Enter the name to be found: ");
String pText = keyboard.nextLine(); // pause for user input
// convert to all upper case
pText = pText.toUpperCase();
}
This is all I have for this
and the output should look like:
Please enter your choice:
1
Enter the portion of a name to search for:
sam
Searching among the males:
Samuel 1880
Sam 1900
Samual 1882
Sampson 1898
Sammy 1880
Sammie 1931
Samson 2009
Searching among the females:
Samantha 1991
Rosamond 1911
Sammie 1933
Samatha 1989
Samara 2006