1.11M Members

ArrayLists

 
0
 

Hi, Im trying to write a program that uses a .txt file to fill an arrayList of type Customer (A class that I made.) and then using that ArrayList to perform other functions within my Customer Database class. when I test my method, nothing is being added to the ArrayList I created. If anyone could help me figure out why that would be great. We're trying to add into the ArrayList from "public void readCustomerData(String CustomerNames)"

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
import java.util.Date;
/**
 * Uses the ArrayList Database and Customer() class to generate data from the customernames.txt file.
 * 
 * @Lindsey Scribner
 * @March 28, 2013
 */
public class CustomerDatabase
{
    //instance variables
    private ArrayList <Customer> database;

    public CustomerDatabase()
    {
        database = new ArrayList<Customer>();
    }

    public void readCustomerData(String CustomerNames) 
    {
        //         Attempt to read the complete set of data from file.

        try
        { 

            File f = new File("CustomerNames.txt");
            Scanner sc = new Scanner(f);
            String logline;

            //read header information first
            logline = sc.nextLine();

            while(sc.hasNext()) 

            {
                logline = sc.nextLine();

                //remove this print statement after method works
                System.out.println(logline);

                Customer c; 

                c = new Customer(logline);

                database.add(c);

            }

            sc.close();

        }

        catch (IOException e) 

        {
            System.out.println("Failed to read the data file: " +
                "CustomerNames.txt");
        }

    }

    public void addCustomer(Customer c)
    {
        database.add(c);
    }

    public int countCustomers()
    {
        return database.size();
    }

    public int countCustomers(int zip)
    {
        int count = 0;

        for (Customer c: database)
        {
            if (zip == c.getZip())
            {
                count ++;
            }
        }

        return count;
    }

    public int countDebtFree()
    {
        int count = 0;

        for (Customer c: database)
        {
            if (c.getBalance() == 0)
            {
                count ++;
            }

        }

        return count;
    }

    public Customer getHighestDebt ( ) 
    {
        int count;
        for (Customer c: database)
        {

        }
        return;

    }

    public Customer getYoungestCardholder ( ) 
    {

        for (Customer c: database)
        {
            Date Todaysdate = new Date();
        }
        return c;
    }

    public String getCardSummary(String card) 
    {
    }

    public ArrayList <Customer> getMailingList(double low, double high) 
    {
    }

    public ArrayList <Customer> getMailingList(String keyword)
    {
    }

    public static void main (String args[])
    {

    }
}
 
0
 

can you give the customer code?

at first glance , bits of your code seems redundant... like

    public void addCustomer(Customer c) {
        database.add(c);
    }

your already doing this inside the try catch block.. so why keep this?

    public void readCustomerData(String CustomerNames) {
    try {

        File f = new File("CustomerNames.txt");

are you trying to append the .txt to the string to make it a filename? i dont know if that can be done... probably this is the reason why it isnt working...

 
0
 

From what I see, you have your code in the readCustomerData method. Every time you call it, new data will come in the ArrayList. Also you don't use the argument of the method. I don't know if this is what you want, but if you want to have the ArrayList private then I would make the following changes.

Remove the method readCustomerData from the class and put it in another class (probably as static)

public class CustomerUtil {

public static CustomerDatabase readCustomerData(String customerNames) 
{
    CustomerDatabase database = null;
    try
    { 

        File f = new File(customerNames);
        Scanner sc = new Scanner(f);
        String logline;
        database = new CustomerDatabase();

        //read header information first
        logline = sc.nextLine();

        while(sc.hasNext()) 
        {
            logline = sc.nextLine();

            //remove this print statement after method works
            System.out.println(logline);

            Customer c = new Customer(logline);
            database.add(c);
        }
        sc.close();
    }
    catch (IOException e) 
    {
        System.out.println("Failed to read the data file: " + customerNames +" with exception: "+e.getMessage());
    }
    return database;
}

}

You can call it like this:

public static void main(String [] args) {
  String fileName = "C:\\Users\\User\\Desktop\\New Folder\\customerNames.txt";
  CustomerDatabase db = CustomerUtil.readCustomerData(fileName);
}

Also, how are you trying to execute your programm? I don't see anything in your main method. If you want to execute anything, you need to put it in the main method and then run that class. It doesn't matter what you have in your class. When you run your class, only what is in the main method executes.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article