wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes help with database! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with database!" Watch "help with database!" New topic
Author

help with database!

christopher persinger
Greenhorn

Joined: Aug 23, 2005
Posts: 20
Hello all, well I am still working on my program and I am on the Database class. I have a load method and i want it to do the following:

1. Load data from customer.txt
2. Read in this data and put it into a Customer List (array list) in ascending order

I am kind of stuck on what to do. I know I need a scanner to read in the file and its contents. But I need it to only keep the lines that match the Customer class requirements (last name, first name, and email). What would be the best way to do all of this? Should I do a while loop or what? How do I keep creating instances of the Customer class with different names (ex. C1, C2 or something like that)?

Below is the code I have so far.. I already have a try loop set up to catch 2 possible exceptions in case the text file cannot be read or loaded. So how can I set up something that will check the parameters of a Customer object? I already have a for loop 'thought up' to use so that I can insert the new Customer objects made into the list in ascending order. The problem is creating these Objects based on the information in the text file. Thanks in advance!!

todd runstein
Ranch Hand

Joined: Feb 15, 2005
Posts: 64
You're asking a couple of questions at once. To see how to use the Scanner to separate lines, and then separate the values of each line, take a look at this http://java.sun.com/developer/JDCTechTips/2004/tt1201.html#1

As far as the naming of the Customer object go, don't name them. Assuming you've pulled out the name, address, and whatever else your text file contains, simply do:

CustomerList list = new CustomerList();
......
list.add(new Customer(name,address,<other args> );


I hope that helps you get a bit farther along. Good luck!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by todd runstein:
You're asking a couple of questions at once. To see how to use the Scanner to separate lines, and then separate the values of each line, take a look at this http://java.sun.com/developer/JDCTechTips/2004/tt1201.html#1

As far as the naming of the Customer object go, don't name them. Assuming you've pulled out the name, address, and whatever else your text file contains, simply do:

CustomerList list = new CustomerList();
......
list.add(new Customer(name,address,<other args> );


I hope that helps you get a bit farther along. Good luck!


Or, if you WANT to use a name, you only need one:


Layne


Java API Documentation
The Java Tutorial
christopher persinger
Greenhorn

Joined: Aug 23, 2005
Posts: 20
I have already created the arraylist.. i just need to read in the text file but only take in the lines that have the right elements to fit the Customer class... which are: lastName, firstName, and email. So how would I set up the scanner to read in only the lines with these 3 elements.. and then how would i create a customer object after i have recieved a line with all 3 of these elements so that i can inset this new customer object into the list??

the text file looks like this:
Charlie,Zed,czed@comp2210.com

John,Smith,jsmith@comp2210.com
123
Sally,Baker,sbaker@comp2210.com
Yo Yo, Ma ,yoyo@comp2210.com
Mike,Bright,mbright@comp2210.com
blah,blah,blah,blah
Alan,Adams,aadams@comp2210.com
Miles;Davis;miles@comp2210.com

So, I only want to take in the lines with a last name, first name, and email.. Also, i need to use a comma as the delimiter..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help with database!