I am new to programming and I am trying to learn, I got a book and it has some problems in it,
Here is one of the problems that I would like some help with if anyone could help me through it
of if you have any code that is similar to what I am looking for that would be greatly appreciated.
The book did not give me any of the answers. Thanks in advance. Josh

I need a lot of help with writing a program that will read a file of text,
and accomplish the following:

1.) Read in a list of names, addresses, zip codes, and phone numbers.
2.) Allow the user to select subgroups on any field (e.g. lists who reside in
the State of Texas, or whose telephone numbers are in the area code.
3.) Maintain alphabetized sublists of the subgroups selected by the user
4.) Print a summary of individuals in each subgroup selected by the user

The idea of those problems is that you solve them yourself so you'll learn something in the process, it's not that you get others to solve them for you so you can show your teacher you've done your homework.

Im actually not in school anymore, this is something that I am wanting to learn on my own and I got a book and there are some questions in it, this was one of the questions that was in the book. I was just looking for some help thats all, a beginning point or some pointers. This is something that does not come easy to me and I was just looking for some help with it. I wish I were still in school then at least there would be someone for me to ask there. But I am not so that is why I have come here, I just heard that this is a really good well-established website on helping people out.

Take a stab at it first, then post what you have. It's easy to do the work for you, but then what would you learn from it? If you are interested in code, then check out something like: http://www.planet-source-code.com/

>I was just looking for some help thats all
We'll be happy to help, when you ask a question that doesn't resemble "Help me with the problem I just pasted but haven't actually tried to solve yet".

>I just heard that this is a really good well-established website on helping people out.
It is. We just don't do your work for you, nor do we take problems cut out of a book and turn them into full blown tutorials. You're expecting too much and that will only result in your being disappointed.

Thanks Guys for all your posts, Im not looking for a hand out, I am sorry that I made it sound that way, its just for me I kind of feel like a blind man running through a mine field. Wondering which way to go where to start how I should start, stuff like that. But I am going to give it my best, and post what I get.

Start from the beginning, and start coding.
You learn little from reading code, you got to get your hands dirty.

Type in those examples (most books have short ones), see what they do and play around with them.
Change them around to see what happens, apply that knowledge the book is trying to give you.

i pasted my problem on the correct forum and included more of the code so that you get a better idea of what im doing. any pointers will be appreciated, thanks!

ok I think I did it alright. Anyone wanna comment please do, all comments are welcome. THanks so much. Josh

Author: Joshua
Language: Java
Date Created: 12-6-2004


The purpose of this program is to prompt a user to search for someone or someplace, or both
and then to show the results of the search.

/**
 * This class holds information about a record. 
 * i.e, will have name, address, city state, zip code, phone number
 * 
 * This class assumes that records will be in following format.
 * 
 * |Name
 * |Address
 * |City, ST ZIP
 * |Phone
 * 
 * ST = Short name  (2 chars) State    
 * ZIP = 5 Number zip code.
 */
public class DataRecord
{
    /**
     * Individual Name
     */
    private String name = "";
    
    /**
     * Individual Address
     */
    private String address = "";
    
    /**
     * Individual City
     */
    private String city = "";
    
    /**
     * Individual State
     */
    private String state = "";
    
    /**
     * Individual Zip
     */
    private String zip = "";
    
    /**
     * Individual Phone
     */
    private String phone = "";
    
    /**
     * Constructor which initializes the record.
     * @param String line1     First line of record from the file.
     * @param String line2     Second line.
     * @param String line3     Third line.
     * @param String line4     Fourth line.
     */
    public DataRecord(String line1, String line2, String line3, String line4)
    {
        setName(line1);
        setAddress(line2);
        setCityStateZip(line3);
        setPhone(line4);
    }

    /**
     * Parse the line 3 to set City state and zip code.
     * @param String line3 third line of record as reaad from data file. 
     */     
    public void setCityStateZip(String line3)
    {
        int index = line3.indexOf(',');

        if (index < 1)
        {
            throw new RuntimeException("Invalid Record. comma not found." +
                "Expected: \"city,ST ZIP\". " +
                "Got: " + line3);
        }

        String tmpLine = line3.substring(index+1).trim();
        if ((tmpLine.length() < 5) || 
           (tmpLine.charAt(2) != ' '))
        {
            throw new RuntimeException("Invalid Record. " +
                "Expected: \"city,ST ZIP\". " +
                "Got: " + line3);
        }

        setCity(line3.substring(0, index));
        setState(tmpLine.substring(0,2));
        setZip(tmpLine.substring(4));        
    }
     
    /**
     * Returns the Individual's city.
     * @return String, city. 
     */
    public String getCity()
    {
        return city;
    }

    /**
     * Returns the Individual's name.
     * @return String, name. 
     */
    public String getName()
    {
        return name;
    }

    /**
     * Returns the Individual's Phone number.
     * @return String, phone number. 
     */
    public String getPhone()
    {
        return phone;
    }

    /**
     * Returns the Individual's state.
     * @return String, state. 
     */
    public String getState()
    {
        return state;
    }

    /**
     * Returns the Individual's zip code.
     * @return String, Zip code. 
     */
    public String getZip()
    {
        return zip;
    }

    /**
     * Sets the Individual's city.
     * @param String string     city. 
     */
    public void setCity(String string)
    {
        city = string;
    }

    /**
     * Sets the Individual's name.
     * @param String string     name. 
     */
    public void setName(String string)
    {
        name = string;
    }

    /**
     * Sets the Individual's phone.
     * @param String string     phone. 
     */
    public void setPhone(String string)
    {
        phone = string;
    }

    /**
     * Sets the Individual's state.
     * @param String string     state 
     */
    public void setState(String string)
    {
        state = string;
    }

    /**
     * Sets the Individual's zip code.
     * @param String string     Zip code. 
     */
    public void setZip(String string)
    {
        zip = string;
    }

    /**
     * @return String, User Address
     */
    public String getAddress()
    {
        return address;
    }

    /**
     * @param String address    UserAddress 
     */
    public void setAddress(String address)
    {
        this.address = address;
    }

    /**
     * Gives String representation of this record.
     * @return String, represntation of this record.
     */    
    public String toString()
    {
        StringBuffer result = new StringBuffer();
        /*
        result.append("Name:" + getName() + "; " );
        result.append("Address:" + getAddress() + "; " );
        result.append("City:" + getCity() + "; " );
        result.append("State:" + getState() + "; " );
        result.append("Zip:" + getZip() + "; " );
        result.append("Phone:" + getPhone());
        */
        result.append(getName() + "; " );
        result.append(getAddress() + "; " );
        result.append(getCity() + "; " );
        result.append(getState() + "; " );
        result.append(getZip() + "; " );
        result.append(getPhone());
        return result.toString();        
    }

}

Author: Joshua
Language: Java
Date Created: 12-10-2004


The purpose of this program is to take user inputs and to read data and to store data, and
subgroups. It also controls the user menu

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * This is the main class. It controls the user menus, take user inputs, 
 * reads the data file and manages the subgroups.
 * It also maintains subgroups selected by the user. 
 */
public class MList
{
    
    /**
     * Reader to read user inputs from command line.
     */
    private BufferedReader in = null;
    
    /**
     * Root/first node of the linked list which holds the Data Records. 
     */
    private List individuals = null;
    
    /**
     * This list will contain the subgroups selcted by the user. 
     */
    private List subGroups = new ArrayList();

    /**
     * Initializes the reader. 
     */
    private void init()
    {
        in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("At any given point of time enter \"EXIT\" to exit the application.");
    }

    /**
     * gets file name from the user & then read the data  
     */
    private void readfile()
    {
        while (true)
        {
            System.out.print("Please enter the path and name of Data File: ");
            try
            {
                String fileName = getLine();
                readfile(fileName);
                break;
            }
            catch (IOException e)
            {
                System.out.println("Please enter a valid file name. ");
            }
        }
    }
    
    
    /**
     * Reads the data file.
     * @param String fileName   Filename of the data file as provided by user.
     * @throws IOException  
     */
    private void readfile(String fileName)throws IOException
    {
        BufferedReader reader = new BufferedReader(
                                        new InputStreamReader(
                                                new FileInputStream(fileName)));
        
        String line1 = null; 
        String line2 = null;
        String line3 = null;
        String line4 = null;
        String blankLine = null;

        individuals = new ArrayList();      
        while(true)
        {
            //Read first line of the record.
            line1 = reader.readLine();
            if (line1 == null)
            {
                break;
            }
            //Read Sec line of the record.
            line2 = reader.readLine();
            if (line2 == null)
            {
                System.out.println("WARNING: Incomplete record at the end of the file.");
                break;
            }
            
            //Read third line of the record.
            line3 = reader.readLine();
            if (line3 == null)
            {
                System.out.println("WARNING: Incomplete record at the end of the file.");
                break;
            }
            //Read fourth line of the record.
            line4 = reader.readLine();
            if (line4 == null)
            {
                System.out.println("WARNING: Incomplete record at the end of the file.");
                break;
            }
            //create a record.
            DataRecord rec = new DataRecord(line1,line2,line3,line4);
            individuals.add(rec);
            //System.out.println(rec);
            //Read blank line.
            blankLine = reader.readLine();
            if (blankLine == null)
            {
                break;
            }
        }
    }

    /**
     * Reads the information enterd by the user on console. It also stops 
     * the program if user enters exit.
     * @return  String, the line entered by the user.
     * @throws IOException
     */
    private String getLine() throws IOException
    {
        String line = in.readLine();
        if ("EXIT".equalsIgnoreCase(line))
        {
            System.exit(0);
        }
        return line;
    }

    /**
     * Start method which manages the follow.
     */    
    private void start() throws IOException
    {
        readfile();
        manageSubGroups();                    
    }
    
    /**
     * Prints subgroup menu
     *
     */
    private void printSubGroupMenu()
    {
        System.out.println("Please select your choice of the SubGroup by field: ");
        System.out.println("1. SubGroup by matching name");
        System.out.println("2. SubGroup by matching Address");
        System.out.println("3. SubGroup by City");
        System.out.println("4. SubGroup by State");
        System.out.println("5. SubGroup by ZipCode");
        System.out.println("6. SubGroup by Phone Area Code(first 3 digits)");
        System.out.println("7. SubGroup by matching phone number");
        System.out.println("8. Print selected SubGroups");
        System.out.println("0. Exit");
        System.out.print("Please enter your choice:");
    }
    
    /**
     * This method Manages Subgroups, prints menu, takes user input and 
     * then prints the summary.
     */
    private void manageSubGroups() throws IOException
    {
        String choice = "";
        int iChoice = -1; 
        String criteria = "";
        String msg = "";
        subGroups = new ArrayList();
        while(!"0".equals(choice))
        {
            printSubGroupMenu();
            choice = getLine();
            if ("1".equals(choice))
            {
                //Matching name       
                iChoice = 1;  
                System.out.print("Enter matching name: ");           
                msg = "Individuals with matching name";                
            }
            else if ("2".equals(choice))
            {
                //matching address
                iChoice = 2;                             
                System.out.print("Enter matching Address: ");                           
                msg = "Individuals with matching Address";                
            }
            else if ("3".equals(choice))
            {
                //matching city
                iChoice = 3;                             
                System.out.print("Enter City starting with: ");                           
                msg = "Individuals with in the specified City";                
            }
            else if ("4".equals(choice))
            {
                //matching state
                iChoice = 4;                             
                System.out.print("Enter State starting with (2 letters): ");                           
                msg = "Individuals with in the specified state";                
            }
            else if ("5".equals(choice))
            {
                //matching zip 
                iChoice = 5;                             
                System.out.print("Enter matching Zip Code: ");                           
                msg = "Individuals with matching Zip code";                
            }
            else if ("6".equals(choice))
            {
                //matching area code
                iChoice = 6;                             
                System.out.print("Enter Phone area code starting with (3 digits): ");                           
                msg = "Individuals with matching Area code";                
            }
            else if ("7".equals(choice))
            {
                //matching phone number
                iChoice = 7;                             
                System.out.print("Enter matching phone number: ");                           
                msg = "Individuals with matching Phone";                
            }
            else if ("8".equals(choice))
            {
                //print all the selected subGroups
                printsubList(subGroups);
                break;
            }
            else if ("0".equals(choice))
            {
                return;
            }
            else
            {
                System.out.println("Invalid choice. Please enter again a Valid choice.");
                continue;
            }
            criteria = getLine();
            
            prepareSubList(iChoice, criteria, msg);
        }
    }
    
    /**
     * Prints all the details of the subgroups selcted by users and the 
     * correspondings sublists.
     * @param List userSubGroups    List of user subgroups    
     */
    private void printsubList(List userSubGroups)
    {
        for(int i = 0 ; i < subGroups.size(); i++)
        {
            ((SubGroup)userSubGroups.get(i)).print();                      
        }
    }

    /**
     * Adds the subgroup selected by the user, prepares the subList associated 
     * with the user selected sub group. 
     * @param int iChoice           User Choice
     * @param String criteria       SubGroup selection criteria
     * @param String msg            Message to be printed
     */
    private void prepareSubList(int iChoice, 
                            String criteria, String msg)
    {
        SubGroupSelector selector = getSelector(iChoice, criteria);
        SubGroup subGroup = new SubGroup(iChoice, criteria, msg);
        
        for(int i = 0 ; i < individuals.size(); i++)
        {
            DataRecord rec = (DataRecord)individuals.get(i);
            if (selector.isEligibleForSubGroup(rec))
            {
                subGroup.addIndividual(rec);
            }
        }
        subGroups.add(subGroup);        
    }

    /**
     * This method creates and returns appropriate selector based on 
     * user choice.
     * @param int iChoice User choice.
     * @param String criteria   Criteria String to used for selection  
     * @return  SubGroupSelector, based on user choice.
     */
    private SubGroupSelector getSelector(int iChoice, String criteria)
    {
        switch(iChoice)
        {
            case 1: return new SelectorByName(criteria);
            case 2: return new SelectorByAddress(criteria); 
            case 3: return new SelectorByCity(criteria);
            case 4: return new SelectorByState(criteria);
            case 5: return new SelectorByZip(criteria);
            case 6: return new SelectorByAreaCode(criteria);
            case 7: return new SelectorByPhone(criteria);
        }
        return null;
    }

    /**
     * Main method as called by the JVM. 
     * @param args
     */
    public static void main(String[] args)
    {
        MList mlist = new MList();
        try
        {
            mlist.init();
            mlist.start();
        }
        catch(Exception e)
        {
            System.out.println("**** ERROR ****");
            System.out.println(e);
        }
    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.