Text Files



TextFileExample.java

Cat.java class that converts to/from .CSV

ReadWriteCat.java

Read chapter 15


Classwork

Part A

(Overall hint: we are dealing with files, and with input that may be wrong, so there will be a lot of try-catch going on.  It is okay to have a try-catch inside a try (or even a catch) but remember you can also have a try with multiple different catches for different types of disaster.)

Create a class HouseListing that stores an address, an asking price, and a number of bedrooms. 

Add to HouseListing method toCSV that  returns a string in comma separated value format, and a method fromCSV that takes a string in CSV format and returns a Houselisting with those values (or null if anything was invalid). Make fromCSV static.   The CSV format should look like:

1313 Mockingbird Lane,312000,5

Also add a (static) method writeFile that takes a List of HouseListings (a List, not an array) and a file name (a name, not a file), and writes the contents of the List out to the named file in comma-separated format (remember to use all our file tools -- you can just copy my file constructor cascades from the example code). The file might look like

1313 Mockingbird Lane,312000,5
23 The Pines,252000,3
6 French Ave.,188000,2
19 Madison St.,239000,3
(hint: think how you would write a method that just printed all the HouseListings in the List (how do we loop through a List?), then set it up so you're printing to the file instead of output, and remember that instead of the result of toString you want the result of toCSV)

Create a class HouseWriter with a main.  In main, create a List  of at least three HouseListings, then have writeFile write out the HouseListings from the List as a number of comma separated lines to a file called "newHouses.csv"  You can use Houselisting.writeFile if you made it static (did you remember to close your file when you were done?)

Note that when you are checking your results, Excel will want to open .csv files, but your life will be easier if you only open them in notepad/text editor of your choice.  You should also be able to open them in netBeans using the Files tab (next to Projects)).  Remember not to have the file open while your program is trying to write/read it.

 Part B

In HouseListing, add a (static) method readFile that, given a file name, returns a List of HouseListings, which is read in from a file of the comma-separated form (so this method should be able to read the file created by writeFile).

You will need to loop through the file's lines with Scanner's hasNext, and then turn each line back into the information for a HouseListing.

If a line of the file doesn't result in a valid HouseListing, print an error and add nothing to the List for that line.

With a text editor, manually create such a text file called houses.csv with at least 10 addresses and put it in the top of your project directory. (hint: stray spaces at the end of a line are your enemy!)  Make sure to put in some lines in the wrong format.

Add a second class with a main, HouseReader.  In this main, read in and print the contents of both houses.csv and newHouses.csv (which HouseWriter wrote out for you).

[EC] In your mutators, throw an IllegalArgumentException (with useful message) if you get invalid data.  In HouseWriter, get info about a house from the user,  and use try/catch and exceptions to force the user to keep trying until they give you good input, and then add the house to your list before writing it out.