Hello.
I am trying to make a program which allows the user to store, manage, add, delete, etc CD Records from a Registry.
The program includes a "CD" class which contains artist name, album name, etc. An arraylist keeps all these CD records saved.
I am trying to get the program to automatically try to load a previously saved ArrayList from a file (if it exists), and then automatically save the now modified ArrayList back to the same file, once the user exits the program.
I am now doing the loading from file method, but I cannot get it to work.
public void readFromFile(String fileName) { ArrayList<CD> reg = new ArrayList<CD>(); //Creates a new arraylist with CD records try { Scanner scan = new Scanner(new File("filename")); while (scan.hasNextLine()) //while file has more data.. { reg.add(scan.next()); //read the next line } } catch (FileNotFoundException e) { System.err.println("File could not be opened!"); System.exit(1); } scan.close(); }
The error I get is "The method add(CD) in the type ArrayList<CD> is not applicable for the arguments (String)".
I understand that the scan is trying to read a textfile and cannot save that line (String) as a CD object. But I have no idea how to solve this.
What should I do? The excersise advices me to use a Scanner for this part, so i'd prefer to follow that guideline. However, other suggestions (such as filestreamer) are also more than welcome as long as they help me solve this problem.
Many thanks in advance.
Regards,
dcdude.