hello, i am trying to read a list of names from a .txt file into a array when the program starts. I am completly stuck on this. this is the code is have so far but no matter what i try its not working. please help.
your program opens, you select option 5 to load the customer/s file and this is where you are stuck.
if this is correct, 1) if the customer data is saved into a file then you should be creating a Customer object 2) it probably would be better to use an ArrayList rather than an array, so you can easily add new Customers. If the customers are to have a CustomerID, a HashMap would be better
this is roughly the changes to your code
to get this to work, create a class Variable Map customers = new HashMap();
create up a Customer class
you need to break up the customer's data from the file (after reading it into 'line') String's split() will do that for you - it returns an array eg String bits = line.split("\\|");\\where "\\|" is a regex for '|', a common delimeter
Creating the customer object Customer cust = new Customer(bits,bits,bits); (probably would check bits.length to ensure it is = 3)
add the customer object to the hashmap customers.put(bits,cust);//where bits is the customersID, or 'key' for the hashmap
If Michael's comments don't completely fix your problems, please tell us what you mean by "it doesn't work". Does the above code compile? If not, what are the errors? I guess I could take the time to compile it, but even then I'm not entirely sure what is supposed to happen when I run it. This is the part that you need to describe the most. What does your program actually do? In other words what happens when you run it? And how does this differ from what you expect? I think this last is the key part for us to be able to help you. Please provide as much detail as you can so we can offer suggestions that might fix it. Otherwise, we will just have to guess at what you intend which wastes your time as much as it does ours.
thanks for the help so far. what i am trying to do is, to get this program to read in any names stored in a txt file (Customer List.txt) and insert them into the array. this program will allow users to add to a list, search the list for a name and delete from the list. I want to add to it buy being able to save and read from a file. It would be better if the program could read the file automatically when it is first ran. hope this clears up what i am trying to do.
posted 14 years ago
Originally posted by John Saito: thanks for the help so far. what i am trying to do is, to get this program to read in any names stored in a txt file (Customer List.txt) and insert them into the array. this program will allow users to add to a list, search the list for a name and delete from the list. I want to add to it buy being able to save and read from a file. It would be better if the program could read the file automatically when it is first ran. hope this clears up what i am trying to do.
Okay, that helps. However, we also need to know what the program ACTUALLY does. Perhaps you can describe what you do to run the program. It looks like you provide a menu to the user. Which of these choices causes the problems? Details such as these will help you (and us) to understand which parts of your code you need to focus on.
the original program was a database program which keeps track of customers. Right now customer names is the only required field. the program can add, delete, search and list all records (names). There are 2 other classes that go along with this program. (sortString, OneWayNoRepeatList) all of this works just fine, what I am trying to add to the program the ability to save/read all records to a .txt file. I created 2 methods, CustomerList and saveRecords. customerList is where i want the program to read all records into the program (array) so that if a user chooses option 1, all records will be listed, including what is in the .txt file. Then if the user chooses to save, the entire list is saved to the .txt file. I dont need the option 5, which was to load the file.
posted 14 years ago
this is the code i have to read from the file:
I know its not complete but im clueless where to go from here.
this is the code for saving the list to a .txt.:
this outputs "OneWayNoRepeatsList@757aef" in the text file. how do i get the records that are stored in the array to save into the file.
posted 14 years ago
It looks like you are trying to write to the file with
What is this variable called next? Apparently it is an array, but an array of what? From the output you recieve I guess it is an array of OneWayNoRepeatsList objects. What does this class do? From the name, I can only guess that it is a list of some kind? Does it store a list of Customer objects or something similar? If this is the case, then why do you have an array of these lists? Do you need to keep multiple customer lists? This is just something to think about even though it's not related to your exact question.
So assuming that OneWayNoRepeatsList is a list of Customer objects, you need to figure out how to iterate through the Customers in the list. In other words, you need to get one Customer at a time and write it to the file. For each Customer object, you will need to get the data that you want to write to the file. It sounds like you will need at least one loop here. You might also want to find a way to break this up into a couple of different methods. For example, one method might write a single Customer object to a given OutputStream. Then another method will repeatedly call the first method with different Customer objects.
Does this help at all? Let us know how things work out for you.