Tony Docherty wrote:Welcome to the Ranch.
Why are you using an array rather than an ArrayList?
Some advice:
Don't catch exceptions and then do nothing, at the very least print the exception stack trace so you get some visual feedback that something has gone wrong.
Always close Streams after you have opened them and preferably do it in a finally clause to make sure it always happens.
Can you explain clearly in words what the last section of code is supposed to do.
yes I would like to use an array list but i have no clue even how to start.
So what I am trying to do in the last part of the code is when the counter (j) goes through each element of the array , I want to check if the element equals to name (bank name) and if it does, as im trying to edit the address information about that specific bank and as rest of its address details come after the name which are in those specific format (mentioned above) I want the elements now to be replaced by the value of the attributes which was taken from the text fields after being entered by the user and clicking the edit button.
So does this text file contain just one set of data (ie one address) or multiple sets of data.
BTW you have shown only 8 fields but in your first post you said there were 9 lines
The OO approach is to have a class that holds all the data for an address called say Address, create instances of the Address class for each address you read in and then store the Address objects in a collection such as an ArrayList.
This is what im am trying to do. All of those codes that are mentioned on my first post are in my class IAddress
Tony Docherty wrote:
This is what im am trying to do. All of those codes that are mentioned on my first post are in my class IAddress
The IAddress class should hold the data for a single address and not all the addresses. It should have getters and setters for each field, a default constructor and possibly a constructor that takes each field (or a map of fields) so you can easily construct complete objects.
You also need another method (it could be a static builder method in this class or in another class) that reads the file, creates objects of type IAddress and stores them in a collection.
This collection is held in another class eg the AddressBook class and it is this class that has methods to find, add, delete etc addresses.
Tony Docherty wrote:The constructor that takes an IAddress as a parameter should copy every field from the passed in object. This technique is an alternative way of cloning an object.
Sorry, could you please expand more on this ?
Back to my problem, where do you think the error is ?
Campbell Ritchie wrote:Welcome to the Ranch
![]()
TD is quite right that you should have a class which encapsulates the address. I don’t think it ought to be called IAddress, however. There is something wrong about the I bit. Also, using the _ character (eg post_code) and type as part of a parameter name are poor style.
I disagree, I am afraid, about the no‑arguments constructor. I think you should not have an address with blank fields, so the best way to achieve that is to delete the no‑args constructor. Every constructor should initialise every field to a “real” value. No "" or No 0 or anything.
As for design: agree with TD. An address does not need to know how many lines there are in the file, nor how many addresses there are. Remove all that sort of code from this class. similarly you should not be editing the text file from inside the address class. All that sort of thing should be done elsewhere.
Your edit method looks more like a constructor than a method. That is the sort of constructor you should have. With all the set methods, you probably don’t need an edit method.
And why have you got house number as an int? If a house number were a number, then it would be twice as far from no 11 to no 19 as from no 11 to no 15. But on most modern streets, it is thrice as far! House numbers are Strings, so you can have 22a or 2½ as house numbers, and I do know people who live/lived at no 2½ and 22a.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |