• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Trying to edit the contents in an array

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, im new to this forum so please bear with me if ive posted this under wrong section.

I am currently trying to edit 1 line of a text file which contains 9 lines.

First i have a method which reads how many lines are there in the text file.


Then I have the code which copies ENTIRE contents of the text file to a single dimension array.



After this I now try to edit one line which is stored inside the array however when I print out the elements of the array after this method is carried out, it still prints whatever it copied from the textfile which can only mean it wasnt able to edit.

Following is the code which im using to edit the contents inside the array


house_no, house_name,streeet and so on are attributes of the class in which this method lies in. Values for those attributes are set by the setter methods from the main method after the edit button is clicked.

I understand there might be alot of other mistakes in my code and i am not very good with java however I have to learn as this is one of my modules at university.

Thank you in advance.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bibek Paudel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Thank you for the reply and yes I would like to use an array list but i have no clue even how to start.

At the moment the text file I am trying to edit contains bank addresses which are in this format

Bank Name
Building Number
Building Name
Street
Area
Post Code
City
Country

In the GUI when the user clicks the edit button, the setter methods inside the class get the value for the attributes from the text fields which has already been entered by the user prior to clicking the edit button.

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 in summary

1) Contents of each text fields are set as each attribute (nametextfield = name , bldgnumbertextfield = house_no , bldgnametextfield=house_name and so on)

2) Counter goes through each elements of the array and checks if it equals to the name
3) If it does, next element will now be = house_no and the one after that will be house_name and so on

I haven't included the last bit of the code which basically write its back to the text file replacing everything but now with the edited information.

Hope this makes bit more sense.

Thanks.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yes I would like to use an array list but i have no clue even how to start.


There are plenty of on-line tutorials on how to use ArrayLists such as this one http://www.java-samples.com/showtutorial.php?tutorialid=234

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.

 
Bibek Paudel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Yes the file is going to contain multiple sets of data which is why I am trying to compare the name of the bank then only editing its address and I have said 9 lines because I have a line separator which is just something like $$$ at the end of an address.

Also

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 however I am calling the edit method or save method via my mainjframe (my main class)
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bibek Paudel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Some thing like this... ?



For instantiation, i have this in the main frame
private IAddress HeadOfficeAddress= new IAddress();

When the user clicks the edit button



>
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

With regard to your general design: When adding variables and methods to a class you need to think whether or not they really belong with that class. For instance an Address class holds an address and may have methods to manipulate an address but should it have methods to read the number of lines in a file? - where/how does that fit in with the concept of an Address class.
Also why should an Address class know how to populate a JTextArea, it may be reasonable to have a method that returns an array of strings that represent how the address would be laid out on an envelope but should it be dealing directly with GUI components?

As an aside: you should use Java naming conventions when writing code as it makes it easier for others to read your code:ie methods and variable names start with lower case letters; concatenated words use camel case ie streettextfield should be streetTextField etc
 
Bibek Paudel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?

Thanks for you advice and before this i was writing my code neatly and was using camel case when using concatenated words until i started falling behind and had so much to do, i started focusing more on trying to get things done than get it little done but neatly.
I really want to get this edit method working. We have 3 hour lab sessions for java and before we broke up for Christmas, it was lab 7 however this stuff im trying to fix is lab 4.

Back to my problem, where do you think the error is ?
When i print out after storing the contents of text file into the array, its printing out correctly however the code doesn't edit the elements as it prints out the same thing when i print out after that bit of code is executed.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sorry, could you please expand more on this ?


In your code you are passing a reference to an IAddress object into the constructor and getting the name from it but you are not getting any of the other fields. If I see a constructor which takes a parameter of the same class type I expect it to copy all the values so I construct a duplicate object.
If you want to be able to construct an object with a name and no address then provide a constructor that takes a single String parameter.

Back to my problem, where do you think the error is ?


I've not even looked at your code to see why it isn't working.
The first thing is to get the design right and then worry about the coding later. If the design is wrong then a huge chunk of the code you write will probably have to be rewritten so it is crucial to get the design right before you start coding.
For instance if you use an ArrayList you don't need your numberoflines method at all - ie the code in that method is redundant.
 
Marshal
Posts: 80505
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bibek Paudel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Thank you Sheriff. At the moment, my goal is to get the basics done and then focus on "real life problems" such as the house number being a string and I have definitely seen 22a and 22b myself but don't think I have ever heard of a number and a half lol
Also, this is me simply following the instructions given to me by my course instructor. We have been provided with the lab sheets and that is how she wants it. Maybe I am coding it in the wrong way however my course instructor has specifically said the edit method and save method needs to belong to the class.

I have attached the class diagram and the default constructor layout which is provided to me which i have to follow. Several students who are far ahead+good at java do question these sort of things but as I am not good at this Im just following the lab sheet provided.
Class-Diagram.JPG
[Thumbnail for Class-Diagram.JPG]
Class diagram and default constructor
reply
    Bookmark Topic Watch Topic
  • New Topic