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

Do you have a question?

How do I search all the text files to display option 1 details

Write code to read lines from each of the files one by one and search each line.
You need to ask a more specific question to get a more specific answer.

Okay sorry, I do not know how to search more than one text file. So I have the option selection for the user and I have a method that takes the user input now I need to search all 130 files for names that have the same characters as the user input , but I am not sure how to do so.

If the names of the 130 files were in an array or array list, your code could go through the list of filenames, open each file in turn, read its contents and search each line for a match with the user's input.
pseudo code:
begin loop to get filenames from list
get next filename
open file
begin loop to read lines
read line
search line
end loop through a file's lines
end loop through the list of files

How would I implement this loop so that each of the files can be stored in my String fname[ ] array?

     String file;

        for(int i=0;i<130;i++){
            String year = "1880"+i;
            file = "names\\yob" + year + ".txt";
        File fileNAMES = new File(file);

            }

What do you mean by storing a file? What are you storing? The name of the file or the contents of the flle?
If the name: assign the filename to the element of the array indexed by i:
anArray[i] = value;

Thanks, I was using this to test my program but now that I am in the later stages I need to check all 130 files and checking all 130 files this way would not be fun.

    String fname[] = {"yob1880.txt","yob1881.txt","yob1882.txt","yob1883.txt","yob1884.txt"};

I am now trying to split the output into males and females by creating a couple of array lists.

     ArrayList<String> Males = new ArrayList<String>(1);
     ArrayList<String> Females = new ArrayList<String>(1);

When the code is executed the code will loop for the amount of files pulling out both Male and Female verisions of a name. And displaying them chronologically but I want to display them by male first and then female. Instead of displaying a mix of males and females as I have below.

    if(name.equalsIgnoreCase(namesToLookup)||name.contains(namesToLookup)){
                        System.out.println(name + " "+(year));

I am familiar with array list but I have not used them an excessive amount so I am not sure what I am doing wrong. If the sex of the names that were returned are equal to "M" then add them to the Males array list isnt that done correctly?

        if(sex.equals("M")){
                        Males.add(name);
                    }else if(sex.equals("F")){
                        Females.add(name);
                    }

Along with this I am trying to display the best year the names had. So when I type in Sam it will go through each of the array lists of male and female and display the best name of that year that consists of the name that the user is looking up

Searching among the males:
Samuel 1880
Samie 1907
Samir 2009
Samantha 1989
Searching among the females:
Samantha 1991
Rosamond 1911
Sammie 1933
Samatha 1989

I know this a lot to help with but I really appreciate the help. And I will understand if people decide not to help.

what I am doing wrong.

Can you explain what the program does?

You'll have to post some code if you are looking for comments on how to change what it is doing.

This code is the property of this poster(FUTURECompEng) and under no circumstances may this be plagiarized, this is intended for educational purposes only.

It needs to take the top 1000 names for males and females from each file(130 total). The search(String target) method in NameCollection takes a string, and searches through all the names for any that contain that substring (not case-sensitive). Then it prints out a line for each matching name to System.out, printing the name followed by its best year. The final output will look like:
1. Search the Names for a string and best year
4. Exit

Please enter your choice:
1

Enter the portion of a name to search for:
sam

Searching among the males:
Samuel 1880
Sam 1900
Samson 2009
Sammy 1946
Samie 1907
Samir 2009
Samantha 1989
Searching among the females:
Samantha 1991
Rosamond 1911
Sammie 1933
Samatha 1989
Isamar 1990

Sorry the code is very messy. I really want to finish this and see where my name ranks!

/*
 * File: Name.java
 * --------------------------
 * This class represents a single entry in the collection.  Each
 * Name contains a name and a list giving the popularity
 * of that name for each year stretching back to 1880.
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Name implements Constants {

    /*private instance variables*/
    private String Name;
    private int[] rankings;
    private Name namesdb;

/* Constructor: Name(line) */
/**
 * Creates a new NameCollection from a data line as it appears
 * in the data file.  Each line begins with the name, which is
 * followed by sex and integers giving the rank of that name for each
 * year.
 */
    public Name(){}
    public void init(){
        //reads the file of names and adds to the NameCollection
        //namesdb = new Name(NAMES_DATA_FILE);
    }
    public Name(String name, int[] ranks){
        Name = name;
        rankings = ranks;
    }
    public String getName(){
        return Name;
    }
    public int[] getRankings(){
        return rankings;
    }

//  public int getRank(int rank){
//      return rankings[rank];
//  }
    public Name(String line) {
        parseLine(line);
    }
/*
    private void parseLine(String line) {
        //gets the name
        int nameEnd = line.indexOf(",");
        Name = line.substring(0, nameEnd);
        //gets the popularity ranking and puts it into an array
        String numbers = line.substring(nameEnd + 3);
        StringTokenizer tokenizer = new StringTokenizer(numbers);
        for(int count = 0; tokenizer.hasMoreTokens(); count++) {
            int popularityRank = Integer.parseInt(tokenizer.nextToken());
            rankings[count] = popularityRank;
        }
    }
    */
    public int bestYear(){ //My attempt
        int best = rankings[0];
        int rank = 0;
        for (int i = 1; i < rankings.length; i++){
              if(rankings[i] < best && rankings[i]!= 0){
                      rank = i;
                      best = rankings[i];
              }
        }
           return rank*1 +1880;
     }


/* Method: getName() */
/**
 * Returns the name associated with this entry.
 */
    /*
    public String getName() {
        return Name;
        }

/* Method: getRank(year) */
/**
 * Returns the rank associated with an entry for a particular
 * year.  The year value is an integer indicating how many
 * decades have passed since the first year in the database,
 * which is given by the constant START_YEAR.  If a name does
 * not appear in a year, the rank value is 0.
 */

    public int getRank(int year) {
        return rankings[year];
    }

/* Method: toString() */
/**
 * Returns a string that makes it easy to see the value of a
 * Name.
 */
    public String toString() {
        String value = "\"" + Name + " [";
        for(int i = 0; i<NYEARS; i++) {
            value += getRank(i) + " ";
        }
        value += "]\"";
        return value;
    }
}

/*
public Name(String name,int numberOfUses) {
     String Names = name;
     Name aName = new Name(name, numberOfUses);

}
public String getName(){
    return null;
    // returns the name
    }
public int getRank(int year){
    return year;
    /*
     * returns the rank of the name in the given year. 
     * Use the convention that year=0 is 1880, year=1 is 
     * 1881, and so on. The Name constants START=1880 and 
     * YEARS=131 define the start year and the number of 
     * years of data.
     */
    /*
}
public int bestYear()
{
    return 0;
/*
 * returns the year where the name was most popular, 
 * using the earliest year in the event of a tie.  
 * It is safe to assume that there will never be more
 * than 150000 births in a single year for a given name.
 */
    /*
}
*/
//}

Start Class

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
//import acm.util.*;
import java.util.*;

/*
 * File: NameCollection.java
 * -----------------------------
 * All of the methods have been created and are ready for implementation, I only did not have
 * enough time to embed them and I did not want to risk by program not working at all.
 */

public class NameCollection implements Constants {

    /* Private instance variables */

/* Constructor: NameCollection(filename) */
/**
 * Creates a new NameCollection and initializes it using the
 * data in the specified file.  The constructor throws an error
 * exception if the requested file does not exist or if an error
 * occurs as the file is being read. I was not able to recover how
 * this was done in the WORDLOOK up program so implemented try as
 * shown in class to do the same function.
 */
    Scanner keyboard = new Scanner( System.in); 
    public static void main(String[] args) throws IOException  
    {
        // create an instance of this class
        NameCollection Instance = new NameCollection();        
        // call a non-static method to do everything else
        Instance.mainLoop();    

        System.out.println("\n" +
           "Exiting program...\n");
    }


    //-------------------------------------------------------------------------
    // mainLoop() - display identifying information and run main loop with menu
    //      The words "throws IOException" have to do with names error 
    //      handling.
    //
    void mainLoop() throws IOException 
    {
        // First take care of creating and initializing the names
        // Define the instance of the names ArrayList
       //  names = new ArrayList<String>();
      //  readInNames();

        String nameText = "";      // stores user Input in main loop
        char[] nameTextArray;        // starts with the nameText

        // Display identifying information
        System.out.println( "Author: Ali Mohammed \n" +
                            "Class: CS 107, Spring 2012 \n" +
                            "Program #6: Name Surfer" +
                            "TA: Shuyang, Wed 9:00-9:50 \n" +
                            "April 26, 2012 \n");                                   

        // Prompt for input to be used
        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();
        if( menuChoice.equals("2") ) {
        //  promptForBestName();
        }
        if( menuChoice.equals("3") ) {
        //  promptForBestName();
        }
        // handle user input for "exit"
        if( menuChoice.equals("4") ) {
            System.out.println("Exit was chosen.");
            // skip rest of code
            System.exit( 0);        
        }
        //promptForStartingName()
        // Handle menu options 1 


        if( menuChoice.equals("1") ) {
            // Selected "1" to search text
            //searchText();
            //findEntry(nameText);
            doSearch();

            // skip rest of code
            System.out.println("\n" +
                               "Exiting...");
            System.exit( 0);
        }
        else if( menuChoice.equals("2")) {
            // Selected "2" to decode using user-entered values, or

            // Prompt for nameText to use
            System.out.print("Enter the nameText: ");
            nameText = keyboard.nextLine();
            // convert to upper case and make it into a character array
            nameTextArray = nameText.toUpperCase().toCharArray();

        }//end else if( menuchoice.equals...
        else {
            System.out.println("Invalid menu option chosen.  Please re-run program");
            // skip rest of code
            System.exit( 0);
        }

    }//end mainLoop()

    /** Given the string which, it prints all the names where it occurs 
    and for each indicates the census when it did best
    */
    public static void displayNames(String which) {
    which = which.toUpperCase();
    for (Name x: records) {
        String name = x.getName().toUpperCase();
        if (name.indexOf(which) >= 0) {
        int year = x.bestYear();
        System.out.println(name.toUpperCase() + "  " + (1880+1*year)); 
        }
    }
    }
              }//end else

    public void doSearch() throws IOException
    {
         ArrayList<String> Males = new ArrayList<String>(1);                                         
         ArrayList<String> Females = new ArrayList<String>(1);
        Scanner input = new Scanner(System.in);
         System.out.print("Enter the name to be found: ");
         String namesToLookup = input.nextLine();    // pause for user input
         // convert to all upper case
         namesToLookup = namesToLookup.toLowerCase(); 
        //int counter=0;
        // assign the filename to the element of the array indexed by i:
        // I was not able to figure out how to create a loop to implement these   //files into this array sorry. The loadData() method is what I am trying to use. 
        String fname[] = {"yob1880.txt","yob1881.txt","yob1882.txt","yob1883.txt","yob1885.txt","yob1886.txt","yob1887.txt",
                "yob1888.txt","yob1889.txt","yob1890.txt","yob1891.txt","yob1892.txt","yob1893.txt","yob1894.txt","yob1895.txt",
                "yob1896.txt","yob1897.txt","yob1898.txt","yob1899.txt","yob1900.txt","yob1901.txt","yob1902.txt","yob1903.txt",
                "yob1904.txt","yob1905.txt","yob1906.txt","yob1907.txt","yob1908.txt","yob1909.txt","yob1910.txt","yob1911.txt",
                "yob1912.txt","yob1913.txt","yob1914.txt","yob1915.txt","yob1916.txt","yob1917.txt","yob1918.txt","yob1920.txt",
                "yob1921.txt","yob1922.txt","yob1923.txt","yob1924.txt","yob1925.txt","yob1926.txt","yob1927.txt","yob1928.txt",
                "yob1929.txt","yob1930.txt","yob1931.txt","yob1932.txt","yob1933.txt","yob1934.txt","yob1935.txt","yob1936.txt",
                "yob1937.txt","yob1938.txt","yob1939.txt","yob1940.txt","yob1941.txt","yob1942.txt","yob1943.txt","yob1944.txt",
                "yob1945.txt","yob1946.txt","yob1947.txt","yob1948.txt","yob1949.txt","yob1950.txt","yob1951.txt","yob1952.txt"}; 
        // need to input the list of file names



        File file = new File("data.txt"); // babies with the same name will be put into a single file for a personal visual aid ignore
        FileWriter fstream = new FileWriter(file, true);
        BufferedWriter out = new BufferedWriter(fstream);
        for(int i=0; i<fname.length;i++){
            File f = new File(fname[i]);
            try{
                BufferedReader freader = new BufferedReader(new FileReader(f));
                String s;
                while((s=freader.readLine()) != null){ // loop continues until the file is empty
                String[] st = s.split(",");
                String name = st[0];
                String sex = st[1];
                String number = st[2];
                if(name.equalsIgnoreCase(namesToLookup)||name.contains(namesToLookup)){
                    System.out.println(name + " "+ sex + " " + number + " "+(fname[i]));
                    out.write(name); 
                    out.newLine();
                }
                if(st[1].equals("M")){
                    Males.add(name);
                }else if(st[1].equals("F")){
                    Females.add(name);
                }
            //  if(Males.equals(namesToLookup)){
            //      System.out.println("male names" + Males);
        //      }else if(Females.equals(namesToLookup)){
            //      System.out.println("female names" + Females);

                }
            //  }
                freader.close();

            }catch (Exception e){

            }
        }
    }
    public void loadData() {
         String file;

        for(int i=0;i<130;i++){
            String year = "1880"+i;
            file = "names\\yob" + year + ".txt";
        File fileNAMES = new File(file);
        //String anArray[i] = file;
            }
    }
}