aspose file tools*
The moose likes Beginning Java and the fly likes reading input from a file into a array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading input from a file into a array" Watch "reading input from a file into a array" New topic
Author

reading input from a file into a array

John Saito
Greenhorn

Joined: Nov 08, 2005
Posts: 5
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.



any help is greatly appriciated.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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[0],bits[1],bits[2]);
(probably would check bits.length to ensure it is = 3)

add the customer object to the hashmap
customers.put(bits[2],cust);//where bits[2] is the customersID, or 'key' for the hashmap

put it all together and you should have it
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Layne


Java API Documentation
The Java Tutorial
John Saito
Greenhorn

Joined: Nov 08, 2005
Posts: 5
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.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Layne
John Saito
Greenhorn

Joined: Nov 08, 2005
Posts: 5
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.
John Saito
Greenhorn

Joined: Nov 08, 2005
Posts: 5
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.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading input from a file into a array