wood burning stoves*
The moose likes I/O and Streams and the fly likes java and files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "java and files" Watch "java and files" New topic
Author

java and files

eleni lekka
Greenhorn

Joined: Jan 03, 2014
Posts: 2
Hi I am doing an exercise and I have some problems.
I have a class Movie and 2 subclasses DayMovie and RegularMovie. You can see my code.



..............................................................................................


...................................................................



..........................................AND MAIN...............





I didnt put set get right now just to be easiest.. Well everything works, but right now i want to take tha m1......m25 from a file not from the main. Can you help me please? Thanks a lot.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2302
    
  49
Welcome to the Ranch

but right now i want to take tha m1......m25 from a file not from the main. Can you help me please?

First of all you should look at using a collection rather than variables m1 to m25. If you use something like an ArrayList to hold your movies you can add as many as you like at a later date without changing your code, with your current way of doing things changing the number of movies will require code changes.

Before we can suggest how to read the data from a file you need to tell us how the data is stored in the file.
eleni lekka
Greenhorn

Joined: Jan 03, 2014
Posts: 2
I don't like either the m1...m25 but its how the teacher gave it to us.
Right now i don't have a file or anything because teacher just told us "put what you thing in a File and the read them from it." Thats why I dont know from where to start.
I know how to put data into files but I dont know how with these and what happening with the subclasses.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2302
    
  49
Presumably now you are reading the data in from a file you can change from m1-m25 to an ArrayList.

You need to decide how you want to structure the data in your file ie CSV, XML etc. Probably the easiest way is to use something like a CSV format with one line of data for each movie. I suggest you google for CSV files.

Reading a line of data at a time is easy (google for something like 'java read line'). Then you need to split the line you have read in at each comma (Hint: the String class has a method for doing this).
You will need to parse each piece of numeric data to convert it from a String to an int (see the Integer class) and then you can construct the appropriate Movie object.
 
GeeCON Prague 2014
 
subject: java and files