• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java and files

 
eleni lekka
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2988
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2988
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic