Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

ArrayList and text file  RSS feed

 
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone. I'm new with java so don't blame me for this question. I was wondering is there a way to compare ArrayList content with text file? Thanks.
 
Marshal
Posts: 61727
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Please explain more of what you want to do; there are probably lots of ways to do such a comparison but you must decide what you are going to do first.
 
Henrikas Butenas
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to have same information in ArrayList and in text fale. So is there a way to compare them? After comparing them I want to add/remove information from file or array
 
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henrikas Butenas wrote:I want to have same information in ArrayList and in text fale. So is there a way to compare them? After comparing them I want to add/remove information from file or array


And if the contents don't match, from which side you are going to crop them, ArrayList or File? Or you going to add to one of these, which one, ArrayList or File? How the decision is being made to delete or remove?

As Campbell told already, tell what is the task in general, don't tell any details about the data structure, i.e.: ArrayList. It is not relevant.

Task may sound:
I have a text file which may contain lots of swear words. The task is to remove them from the text, given that I have the swear words provided by the...

As you described the problem now, it doesn't make sense as simply not clear how to make contents same, we don't see the rationale behind how to make them same. That's probably because not clear what is the task about.
 
Ranch Hand
Posts: 95
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What will be your criteria for adding or removing. Are you going to remove the duplicates...etc?

You can read data from the file and put in the arraylist, based on your criteria, you can remove what you need and write back the update to the file.
 
Campbell Ritchie
Marshal
Posts: 61727
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . . The task is to remove [the swear words] from the text, . . . .

That proves we need more information about the task, because I thought it was something completely different.
 
Liutauras Vilda
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Liutauras Vilda wrote:. . . . The task is to remove [the swear words] from the text, . . . .

That proves we need more information about the task, because I thought it was something completely different.


That just shows that we both don't understand what the task is. I just pulled this example from the thin air.
 
Henrikas Butenas
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I will try to explain the task. So I have a building(object), with address, floor level, year of construction and apartments number on one floor. I have to add/remove/edit these objects and I want to have same information about them in ArrayList and in .txt file. And if the contents don't match i will crop from txt file
 
Liutauras Vilda
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you failed to explain the task. But let me help with some extra questions.

Henrikas Butenas wrote:So I have a building(object), with address, floor level, year of construction and apartments number on one floor.


1. In which form you have it?

2. Where the ArrayList comes from? Does it come with populated information already?

3. Where the file comes from? Does it come with populated information already?

4. Most important. What contains the right information on which based you are going to make decision either you need to ADD or DELETE from either ArrayList or File?
 
Liutauras Vilda
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henrikas Butenas wrote:So I have a building(object)


In case you have building object in the form of Java object, why do you need to add/remove/edit ArrayList.

Can't you just always ADD to array list the information from Building object, which I presume have correct information?

Can't you do the same with text file, instead of trying to edit something, can't you just write new file with correct information from Building object?

or there is something else you didn't tell us, for instance: text file contains all the city buildings, so you need to find the right one and update with correct information as the current one might be outdated among other existing within a file building objects.
 
Campbell Ritchie
Marshal
Posts: 61727
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you add or delete something in the middle of a text file at all? I thought you could overwrite the entire file or append to its end and nothing else.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!