Question: need help with method. This method is supposed to load a .txt file that is named test1.txt which already has info like so: 1 13233
need help with method. This method is supposed to load a .txt file that is named test1.txt which already has info like so:
1 13233 SIks 3209432 Heloo N
where the 1 indicates the row and 13233 is a String, Slks is a String, 320... is a String, and Heloo is a String and the method is supposed to read this file and store in obj called book for each row where there exists multiple rows of data like above.
public void loadData(String filename) { Scanner reader = null; try { reader = new Scanner(new File(filename)); Book book = null; // read until there are lines in the file while(reader.hasNextLine()) { book = new Book(reader.nextLine(), reader.nextLine(), reader.nextLine(), reader.nextLine(), Integer.parseInt(reader.nextLine())); reader.next(); // to escape the blank line in file booklist.add(book); } booklist.sort(null); // sorting the books reader.close(); } catch(IOException e) { System.out.print("Error! Unable to load the file. "); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
