File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Reading from file & processing data. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading from file & processing data." Watch "Reading from file & processing data." New topic
Author

Reading from file & processing data.

Eric Kenton
Greenhorn

Joined: Mar 23, 2013
Posts: 4
Hey guys, first time posting here, so go easy on me

I am trying to create a program that will read in customers data and store it in array. Now I can get the read from a file working just fine and I can also store into an array of Strings, but looking a bit more ahead I think, the way that I have done it is not that great. What my program needs to do is read in 140 customers details, name, surname, age, address and mobile number. The problem that I am having is, at the moment my program reads the text file and then stores all the details in one index of array, so name, surname, age etc, all gets stored in array index 0 for that single customer, next customer in array index 1 etc.

Problem is later on when I go about looking up for specific information such as surname, age or mobile number, everything is stored in index 0 so when I want to look at specific type of information, the program would only return the whole string, which is everything about that customer. Basically I am asking how I would go about storing all the information on one index but have a way of just calling a piece of information that I need, such as telephone number without bringing up everything about that customer, name, surname, age etc. I also can only work with arrays so no list or array lists etc.

Any help would be greatly appreciated.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Define a Customer class with the relevant fields. Store Customer objects in the array. If you just want the phone number, use something like arr[0].getPhoneNumber() (assuming you've defined the relevant getter methods).
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2359
    
  50
Welcome to the Ranch
Eric Kenton
Greenhorn

Joined: Mar 23, 2013
Posts: 4
Jeff Verdegan wrote:Define a Customer class with the relevant fields. Store Customer objects in the array. If you just want the phone number, use something like arr[0].getPhoneNumber() (assuming you've defined the relevant getter methods).


Thanks for the reply, I get some of what you said, but I can't seem to make it work correctly, I think my syntax is not correct. I am a novice to Java and I have hardly ever used getters and setters and I have not yet stored objects in array yet, which would partially explain why I am getting stuck.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Eric Kenton wrote:
Thanks for the reply, I get some of what you said, but I can't seem to make it work correctly, I think my syntax is not correct. I am a novice to Java and I have hardly ever used getters and setters and I have not yet stored objects in array yet, which would partially explain why I am getting stuck.


Okay, but I hope you realize that there's nothing there anybody can use to help you any further. You need to TellTheDetails(⇐click) of what you tried and what problems you encountered. And that's not Java, but just basic communication.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8176
    
  23

Eric Kenton wrote:Thanks for the reply, I get some of what you said, but I can't seem to make it work correctly, I think my syntax is not correct. I am a novice to Java and I have hardly ever used getters and setters...

Then I suspect that now is the time you should read up on them before you write another line of code.

and I have not yet stored objects in array yet...

Sure you have. You already said that you were able to store lines into a String array; and a String is an object.

All you need is a bit of extra logic to convert a line into a Customer, and store that in an array.

However, Jeff's quite right: we need more information if you want more help - specifically, whatever code you've tried; providing it's not the Java equivalent of War and Peace.

Winston
Eric Kenton
Greenhorn

Joined: Mar 23, 2013
Posts: 4
Yeah sorry about that guys, I know I explained the problem really badly. Been playing around with it and I got the getters and setters working correctly now, but now I am stuck on how to split up a string, so that I can assign a one customer name to a setter called name variable. At the moment, all that I have managed to do was to save all the customer names in the file to the setter called name, all at index 0 of the array, if that makes sense. So all my customers names, 140 of them are saved to the name variable at index 0 of my array. Here is a bit of code of my program.

Clearly I am doing something wrong, using the System.out.println(save[0]).getName()); only outputs me all the names in the file. I know that there is a problem in the loop, I am just unsure of how to fix it.

The way I want to do it, is make an one object per customer, and that object will hold the name, surname, age etc and then save that object to an array at index 0 and so on. In order to do this, I think I need to split up each individual line that I read in and assign it to a setter variable.

Hopefully I haven't confused you guys.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2359
    
  50
Why do you have a class called Getters, surely it ought to be called something like Customer ie a name that describes what the class is modeling.
Why do you have an array of type Object, why not use create an array of the type of the objects you want to store in it ie Getter or better still use an ArrayList so you don't need to change your code if the number of customers in your file changes.

So all my customers names, 140 of them are saved to the name variable at index 0 of my array.

Your problem is down to the fact that you are adding the same instance of Getter to every element in the array. You need to create a new instance each time through the loop.

Clearly I am doing something wrong, using the System.out.println(save[0]).getName());

That will print the name of the object at index 0 each time through the loop. The reason it outputs each name in the file is you are changing the data in the one instance of Getter that you have.
Eric Kenton
Greenhorn

Joined: Mar 23, 2013
Posts: 4
Thanks tony for giving me that feedback, it actually opened my eyes and I could see what I was doing wrong. I finally managed to get it working as I wanted.

Thanks guys
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2359
    
  50
My pleasure and well done for getting it working.
 
 
subject: Reading from file & processing data.