• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having problems reading data from a file and filling an ArrayList

 
Ranch Hand
Posts: 94
3
Eclipse IDE Oracle AngularJS C++ Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello fellow programmers. I need some guidance please as to how to fill an ArrayList. Please understand I do not want the whole program to be written for me I'm just stuck in filling the ArrayList and I would like a clarification as to what I am doing wrong.
This main menu is not complete yet as I am focusing on the actual code first. My post is lengthy but I only have one question stated further below. Please do help!



Here is the curator class



Museum class. Here I invoke the readPainting method so iterate through a list of items for all of the museums. I need to read each line and assign it to a variable and then I think I need to put these values into the array paintingArray(line 7) from the museum class but because I have doubles and integers I do not know how to do that. When I call the method painter from the curator class from line 36 I need to pass the name of a painter that the user chose and also the museum filled with a given array that the user also chose. In this case, assuming the array has the information from the last picture I posted, I need to search for all of the paintings from that painter. But since I don't know how to fill the array I am stuck. Please help me understand the process. I have just started to use ArrayList so I just have a basic understanding of it.



And painting class



TextFileIO class (This has some information from another program that I was using it with but had to create it in order to read from a file)



Here is the data saved for each museum and a few lines for one of them


 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Andres wrote:My post is lengthy but I only have one question stated further below. Please do help!


Yes, the post is quite lengthy and I just spent a minute scanning through trying to find that one question you said would be stated further below which you somehow actually forgot to state. What exactly was your question?
 
Daniel Andres
Ranch Hand
Posts: 94
3
Eclipse IDE Oracle AngularJS C++ Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to read each line and assign it to a variable and then I think I need to put these values into the array paintingArray(line 7) from the museum class but because I have doubles and integers I do not know how to do that. This written right before the museum code.

That's what I need to find out. Could you run the code please so that you can see the error message I get?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Andres wrote:Could you run the code please so that you can see the error message I get?



It would be a LOT more practical if you just told us what the error message is. Better still, copy it and paste it into your next post.
 
Daniel Andres
Ranch Hand
Posts: 94
3
Eclipse IDE Oracle AngularJS C++ Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll do that. I've done that in the past but then programmers wanted me to post the code so that they can run it and see it for themselves. I figured that's the preferred option.

I'll post it shortly
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, when you get an error message the first thing to do is to read it. The error message has words in in for a reason, namely to try and tell you what went wrong. Frequently it's possible to fix the problem just based on what the error message says. So asking somebody else to spend several minutes copying and pasting your code just so they can see the error message you've already seen is... well, as I said there's a better way.
 
Daniel Andres
Ranch Hand
Posts: 94
3
Eclipse IDE Oracle AngularJS C++ Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you like to create an exhibition?
yes
For this exhibition, from which Museums would you like the art work be drawn?

Please enter their numbers on one line:
1) National Gallery
2) Phillips Collection
3) Corcoran
4) Portrait Gallery

1
Now that you have chosen the Museums,
please choose an exhibition topic
and enter its number:
1) Painter
2) Year painted
3) Size
4) Medium
5) Country
6) Genre
1
Please enter a Painter:
giotto
Museum@55f96302
nullMuseum@55f96302

------------------------------------------------------------------------------------

I have fixed and added code but my main issue remains. As you can see, museum from museumsUsed is not displaying the correct results. All of the iterations from readFile in the Painting class I need to fill in an ArrayList but because it contains int and doubles I do not know how to do that. I think this is too lengthy and difficult to explain :/
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Andres wrote:
Please enter a Painter:
giotto
Museum@55f96302
nullMuseum@55f96302

------------------------------------------------------------------------------------
I have fixed and added code but my main issue remains. As you can see, museum from museumsUsed is not displaying the correct results.


By "not displaying the correct results" are you referring to the part that I have bolded?
 
Daniel Andres
Ranch Hand
Posts: 94
3
Eclipse IDE Oracle AngularJS C++ Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Daniel Andres wrote:
Please enter a Painter:
giotto
Museum@55f96302
nullMuseum@55f96302

------------------------------------------------------------------------------------
I have fixed and added code but my main issue remains. As you can see, museum from museumsUsed is not displaying the correct results.


By "not displaying the correct results" are you referring to the part that I have bolded?



Yes, I have seen that error before when working with regular arrays but I do not remember why this happens.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Andres wrote:
Yes, I have seen that error before when working with regular arrays but I do not remember why this happens.


That's not an error. That looks like the string representation of a Museum object.  Go over you code again and see where you're printing a Museum object. That's where that output is coming from.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daniel, I have to say that the amount of code you posted and the actual source of the problem you're trying to find indicates to me that you have a very poor grasp of what your code is actually doing. This is never a good thing. To have that much code and so little understanding of it can only lead to bad things. I guess that's something all programmers have to learn the hard way though.

One way out of this jungle of confusion is refactoring. That is, improving the design, and in particular, the clarity and organization, of your code.

This reply is going to be a long one so I will break it up into a few separate posts.

Buckle up.

(continued...)
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, let's look at what is not working. This is where most of your confusion and lack of understanding becomes apparent.

Your approach for reading in data from a text file and creating objects from it is somewhat unconventional. For a program of this size and complexity, however, it's still relatively manageable. So, we'll stick with this approach for now. Just so you know, Museum is what is known as a domain object. You normally should not assign the responsibility of reading data and creating objects to a domain object. That's a responsibility that's best given to something else in the data layer of your application. Creating objects from data read from a text file is an infrastructure layer concern, not a domain layer concern.

Data in text files like the ones you're reading in presents some challenges:
1. The semantics (meaning) of each line is implicit. You have to know what each line represents.
2. The logical organization of the data is also implicit. You have to know which lines go together.
3. There is very little room for error in 1 & 2. If any of these are off in some way, an automated process that assumes correctness will fail.

As such, we will just trust that the data files you were given do not have any errors. This leaves the possibility of errors happening resting solely on your code that reads it in and interprets its meaning. For future reference, just remember that the conventional approach would be to have something in the application's data layer read in the data file, interpret the data, and build object(s) out of whatever it reads.

(continued...)
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the image you posted and from the code you wrote, this is what I can deduce about the data in those text files:

I have already noted some of the challenges this type of data presents. One thing you don't want to do is compound those challenges by spreading the logic for reading in and interpreting that data across a wide area in your code. This is exactly what you have done, however. The problems caused by your approach is exactly why it is preferable to assign that responsibility to something in the data layer of the application. Those problems include: lack of focus in the code, which leads to a lack of clarity in the code, which leads to a lack of understanding of what the code is doing, which leads to bugs, which leads to difficulty in maintenance and getting the program to work correctly in the first place, which leads to ... (a slow death spiral marked by pain and despair).

(continued...)
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now, let's look at some of the things that you did that adds to the confusion in the code.

Let's start with some basic things that you can easily fix. Let's talk about names. In particular, let's talk about using good names in your program.

You may think this is a very trivial thing but try to take the Stroop Effect test first and see for yourself just how much a conflict between words and intent can affect your brain and its ability to comprehend and function.


"scan museum file" is an imperative sentence; it's a command to do something. A Scanner is a noun, a thing that does something.

Now, imagine naming your dog "Sit".  How the heck are you going to train that dog to do anything? It will be the most confused dog ever. Imagine yourself giving commands to your dog, Sit: "Sit, fetch!" "Sit, run!" "Sit, stay!" "Sit, heel!" "Sit, sit!" "Sit, roll over!" "Sit, play dead!" "Sit, shake hands!" "Sit, sit!"

Do you get what I mean? This is the same kind of confusing thing that the next line of code says:

although I can understand how you might think that line of code actually makes sense. To anyone else who doesn't have the ability to get into your head, however, that line of code can be confusing. At a subconscious level, that line of code may actually be leading you astray as well.

The more insidious problem with the name(s) you use is that it shifts from being a "paintingFile" to being a "museumFile" (line 11). So which one is it? Is this a museum file or a painting file? Because according to your class design, Museum and Painting are two different things. This, as I said, indicates some level of confusion on your part.

There are a number of sensible alternatives to that name. Here are a couple, just off the top of my head:

Notice the suggestions for the other name as well.

(continued...)
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to have to step away for a while, so I'll just cut to the chase. Your main problem is that you read in the data but you never actually add any objects to your List<Painting>.  Look carefully at the code that I originally cited in my second post in this series.

I will go into more details later when I have time.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code:

This is what I was saying about spreading out the logic that reads from the data source across a wide area in your code.  One line 17, you read an integer. Since this is the first number you read from the file, it is supposed to represent the number of data groups that follow it, with each data group comprising information about a single Painting. The rest of the logic to read the data group for a single painting is somewhere else, in that readPainting(read) method call.

Once again, there's a problem with the names you use. The name paintingNumber does not accurately reflect the actual meaning/intent of the value that is assigned to it. This number is not a "painting number". Rather, it is the number of Painting objects that you should expect to create from the data read from the file.  Also, the call to getPaintingInformation.readPainting(read) just doesn't make any grammatical sense at all. What does this code really want to say?

Refactoring the names in this method, I can get a little bit more clarity out of what the code is doing:

Now that it's a little clearer what this code is doing (the code actually tells a story, right?), isn't it more clear that you haven't actually added the new Painting object to a List<Painting>?

Also, it's clearer now that the initializeFrom(source) method call somehow "knows" or assumes to know our current position in the data being read from the source.  If you go into that method, you see all those nextWhatever() calls that assign values to the Painting fields. The problem with this, again, is that the logic for interpreting the meanings (semantics) of the data items and their arrangement in the text file is now spread across two methods.  You have to bounce around between two places of your code to see exactly how this tracking of your place in the source data stream is managed. Having all that logic in one place is better.  It makes the code more coherent.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, there are other names you can consider using besides createPaintings(Scanner source).  There are also other designs you can consider:

Another reasonable method signature would be

Those names at least express what is happening in the method.

Try to figure out how you would call a method that is designed this way.
 
Daniel Andres
Ranch Hand
Posts: 94
3
Eclipse IDE Oracle AngularJS C++ Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:By the way, there are other names you can consider using besides createPaintings(Scanner source).  There are also other designs you can consider:

Another reasonable method signature would be

Those names at least express what is happening in the method.

Try to figure out how you would call a method that is designed this way.



First off, I would like to THANK YOU immensely for such detailed response and tips on the way I have added code to the program. Programmers like you help others get better along the way. This is a project I have to code and this is the way it was designed. I was given a driver and while I too agree that it would have been better to read the file onto an array from the data layer of the application( which I just learned is the way to address this way of coding), I think the reason we need to spread the manipulation of data between classes is to practice on how to do so and how it works. This is exactly why I am having such a headache with this program. I did not know how to fill that data into the array and then return it to the calling method as you stated:


Also, the "throwing away" part is what my main question was and I see everything clear now from your last post. I wanted to clarify that I do understand what is happening in the program, to a good extent so far at least until I figure later on what certain methods are supposed to do as I progress with the code, and the reason everything is unorganized is because I was given the name of the methods with very little detailed in a few of them to get started and I have to start programming each part and connect them to other methods and helping methods. I enjoy working on a program from scratch using my own algorithms but again, I believe the purpose behind this project is to help us manipulate objects from class to class. Thank you for the tips on proper naming of variables and parameters! I shall continue now with the program...
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Andres wrote:First off, I would like to THANK YOU immensely for such detailed response and tips on the way I have added code to the program.


You're welcome. A lot of us hang out here because we like sharing what we know and it's rewarding for us to see our advice help others.

I think the reason we need to spread the manipulation of data between classes is to practice on how to do so and how it works.


You ought to practice doing the right things, not the wrong things. As every music teacher I've ever met likes to say, "Practice only makes habit. Only perfect practice makes perfect."  Do you really want to make a habit of doing the wrong things?

Thank you for the tips on proper naming of variables and parameters! I shall continue now with the program...


Keep practicing, but make sure you're practicing how to get better. Don't just do things because you're asked to do them a certain way. Think critically about what other people do in their code but think even more critically about what you do in your code.

Good luck.
 
Daniel Andres
Ranch Hand
Posts: 94
3
Eclipse IDE Oracle AngularJS C++ Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried to read the data from one museum' array containing a particular painter's name without any luck. I have added the following code:


In the code above(argument), museumArray was added a specific museum

When I invoke this method (below) I feel like I should somehow read through the items in arrayList containing the paintings from National Gallery and then iterate in a loop whether that array contains the painter's name and then return a new String with only those paintings. I'm having trouble relating museumsUsed in the curator class and paintingArray in the Museum class.

In essence I am trying to search through the entire list of National Gallery, for example, and look for only the paintings for a given painter and add them to a new String or array and return it to the calling method.
 
Daniel Andres
Ranch Hand
Posts: 94
3
Eclipse IDE Oracle AngularJS C++ Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not mean to include the in line 3. That was just part of the things I was trying to do
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daniel, I hate to sound negative but I believe you've reached an impasse. You have way too much code that has a lot of things wrong with it, from its basic design, organization, and logic. It's not just a matter of fixing one or two statements or even one or two methods. It's the proverbial whack-a-mole problem where fixing one thing locally will lead to other problems surfacing in surrounding code.

And this is the problem with writing too much code all at once that doesn't work. You develop an attachment to the code and feel like throwing any significant portion away is something you just can't bring yourself to do. However, the more you try to patch things up and pull it together, the more problems you create and the further you get from actually being done. If you're lucky, you may get to point where it will mostly work but you're very unlikely to get it working 100% correctly unless you do a major overhaul and redesign. I don't know if you have the luxury or ability to do that though.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the main problems I see in your code is a lack of focus and specific responsibility. Object-oriented programs are all about that. If It were me, I would have one separate class that was responsible for reading and interpreting a museum data file. It would have a method to report how many painting data groups to expect to find in the file, another to read a painting data group and create a Painting object from it, and another to control the loop that reads in all the Painting data groups and collect all the Painting objects created in a List.
 
Daniel Andres
Ranch Hand
Posts: 94
3
Eclipse IDE Oracle AngularJS C++ Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright man. Thanks again for the suggestions.
I'll keep working on it until is fixed.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I wrote:If It were me, I would have one separate class that was responsible for reading and interpreting a museum data file. It would have ...


Here's an outline of that class, just off the top of my head:

As I said earlier, you could start with all the above code in your Museum class but that's not how you would do it in the real world. In a non-trivial real-world application, the responsibility of reading data from a text file or whatever persistent storage you use, whether its XML, JSON, a relational database, or whatever else, is assigned to a separate class that belongs in the Data Access Layer of your application.
 
Daniel Andres
Ranch Hand
Posts: 94
3
Eclipse IDE Oracle AngularJS C++ Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fixed and working!
Thanks for the help
 
Let's go to the waterfront with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic