I could really do with some help on an assignment. This is the code i have so far. Next, I am trying to write what is in the input file, into the output file. I would be very grateful if anyone could help.
package booksca; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; public class BooksTest { public static void main(String[] args) throws FileNotFoundException, IOException { //Creates an ArrayList ArrayList list = new ArrayList(); Scanner console = new Scanner(System.in); //Prompts the user to enter the input file name System.out.print("Input file: "); String inputFileName = console.next(); //Prompts the user to enter the output file name System.out.print("Output file: "); String outputFileName = console.next(); //Creates a new File object File inFile = new File(inputFileName); try { //Constructs a new Scanner object Scanner in = new Scanner(inFile); while (in.hasNextLine()) { FileReader fileReader = new FileReader(inputFileName); BufferedReader bufferedReader = new BufferedReader(fileReader); String title = in.nextLine(); String authorFName = in.nextLine(); String authorLName = in.nextLine(); double price = Double.parseDouble(in.nextLine()); int year = Integer.parseInt(in.nextLine()); //Creates a new Book object Book b1 = new Book(title, authorFName, authorLName, price, year); System.out.println(b1.getTitle()); System.out.println(b1.getAuthorFName()); System.out.println(b1.getAuthorLName()); System.out.println(b1.getPrice()); System.out.println(b1.getYear()); list.add(bufferedReader.readLine()); } } catch (FileNotFoundException ex) { System.out.println("File not found: " + ex.getMessage()); } //Constructs PrintWriter object with the name if the output file, in order to write to the file PrintWriter out = new PrintWriter(outputFileName); /* int lineNumber=1; while (in.hasNextLine()) { String line = in.nextLine(); out.println(" "); in.close(); out.close(); } */ } }
package booksca; public class Book { private String title; private String authorFName; private String authorLName; private double price; private int year; public Book(String t, String fn, String ln, double p, int y){ title = t; authorFName = fn; authorLName = ln; price = p; year = y; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthorFName() { return authorFName; } public void setAuthorFName(String authorFName) { this.authorFName = authorFName; } public String getAuthorLName() { return authorLName; } public void setAuthorLName(String authorLName) { this.authorLName = authorLName; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } }