• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read in a file outside Main class  RSS feed

 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make an object oriented address book, where I randomly generate name, address, phone number, and output it into a CSV file. I have created a lot of the classes to format the names and addresses, but I want to create a class that will read in the files that are holding my sample names and addresses. The names file is a .txt file and the address is a .csv file. I have posted what I have coded so far. I am experiencing an error in my readFile class, and I have a feeling it is because I am not throwing an Exception, but I'm not sure how to do that outside of the main class. I would also like to add what I read in to an ArrayList. I have posted my code below.







 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We will need the full error message and the stack trace to continue.
 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At this point the main error that I am seeing is in the main class when I am trying to read in the zipcode file by calling the loadCities method in my AddressGenerator class. It says that the loadCities method want to have a String input, which I can see, but when I try to change it to (File inputFilePath) I get an error that it won't compile through. As you can probably see here, I also have an ArrayList<String> zipCode object in the main class and in the AddressGenerator class. This is because I am not sure how to initiate getting the zipcode file read in the main class. Once I figure this aspect out I know I will be good for the rest of the program, but I am just not sure how to get the file read and then added to the right arrayList.
 
Campbell Ritchie
Marshal
Posts: 56597
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remind yourself about the types required by methods and constructors. Look at File for example, and you find it has four constructors. One takes a String as a parameter, and another a URI. So if you try passing something to those constructors other than a String or a URI or their subclasses (which there aren't any of), the compiler will realise you are passing the wrong type and not let you continue with that mistake. It is telling you that you can't create a File from another File. There are two other constructors, but they won't take a File reference on its own. I think the parent parameter means the directory your file is in, and the child parameter the short name of the file.
The File class is telling the compiler, “You will have to pass me those pieces of information in that order, otherwise you can't have an instance.”
And even if you provide that information, a tiny spelling error may guide the constructor to a non‑existent file, and you still can't have an instance.

Pass those pieces of information in the right order, and also go to the Java™ Tutorials, where you find that File is now regarded as legacy code; it will tell you the newer way to find files. While you are in the Java™ Tutorials, have a look at this section about how to close files even if there is an exception. You must close all files you open, otherwise there is a risk of a resource leak.
 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for that response, I keep seeing in all the tutorials you sent that if I want to input a pathname, I have initialized my loadCities class correctly by saying it should be taking in a String. All path or pathname inputs want it to be a string and yet when I tried to load it in from my main class, it is saying that it won't load in the AddressGenerator class because it is expecting a string. Am I initializing this incorrectly, or is it because I am trying to start an arrayList form my mainclass and try having it read in the file from there? I realize that I initialized the same arraylist in two different classes, which is what I am thinking is causing the error, but I am not sure if I should be making the zipCode arrayList in the main class or the addressGenerator class.
 
Campbell Ritchie
Marshal
Posts: 56597
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to try the code, which is awkward, and the one compiler error I got was that you are trying to create a List from loadCities, which has void return type. You can't do that. I think the filling of the List belongs elsewhere. That error sounds different from what I thought at first.
There is lots wrong with the code apart from that.
Your inheritance is wrong; the way you have written it two fields in the Address class are inaccessible both for writing and for reading, and the other is a CSZ, so you cannot say that a CSZ “IS‑A”[n] Address.
I think you shou‍ld be creating the List in the address reading class, filling it in the load cities method and then writing its contents to a file in a different method. You shou‍ld be all right for memory until you have well over 1,000,000 addresses in the List. You shou‍ld move as much code as possible away from the main method.
 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, having loadCities being void is what was causing that error. I do plan on having the main class being as empty as possible, I just have the code written in there right now for testing. I also have separate classes address and phones, so I am hoping those will be okay. My main problem currently is simply getting the file to read and then have the contents of the file transfer over to an array list that I will be able to generate random address from. I'm not really sure how to write a function that sends contents from a file in one method and then writes it to a different method. I've never done that before, but I will look into it.  The ultimate goal is having the files that has the addresses and a separate file that has names, be read and written to a different class that I will be able to generate random info and create a phoney address book from.
 
Campbell Ritchie
Marshal
Posts: 56597
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Sak wrote:. . . I do plan on having the main class being as empty as possible, I just have the code written in there right now for testing.
No. Get the code out of the main method and into a testing method straight off. I always worry when people say they are planning to do something: will they ever actually do it?
I also have separate classes address and phones, so I am hoping those will be okay.
You haven't shown us Phone, and I have already told you that Address is not at all right.
My main problem currently is simply getting the file to read . . . I'm not really sure how to write a function that sends contents from a file in one method and then writes it to a different method. . . .
That may be because you are trying to do too many things all at once. Get a method which reads all the lines from the file and simply prints them out. Make sure it closes all resources. Once you have got that working, you will know you can read from the file. Then you can enhance it to add everything to your List. Then you can consider the nest part. Then you can...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!