Hello, I thought I was done with this program but for some reason it just isn't working. I am required to read from a .txt file, and then write the information to two different .txt file locations. The location depends on the data of course! My text files are being created but instead of data inside them they are empty. Here is what I have in my main:
public static void main(String[] args) throws IOException { Fundraising athritis = new Fundraising("Athritis Foundation", 25000); Scanner input = new Scanner(new File("Donations.txt")); PrintWriter outputPlatinumDonor = new PrintWriter("PlatinumDonors.txt"); PrintWriter outputDonor = new PrintWriter("Donors.txt"); while(input.hasNext("Donations.txt")) { String name = input.nextLine(); String addressline1 = input.nextLine(); String addressline2 = input.nextLine(); double donationAmount = input.nextDouble(); // this is part of the problem domain, each fourth line of text is a double, and that double determines where the information is written to. if(donationAmount >= 1000) { athritis.writeToFile(name, addressline1, addressline2, donationAmount, outputPlatinumDonor); } else if(donationAmount < 1000) { athritis.writeToFile(name, addressline1, addressline2, donationAmount, outputDonor); } } input.close(); }
And here is the writeToFile method that I have made (using a method in another class was required)
public void writeToFile(String name, String addressLine1, String addressLine2, double donationAmount, PrintWriter file) throws IOException { PrintWriter outputPlatinumDonor = new PrintWriter("PlatinumDonors.txt"); PrintWriter outputDonor = new PrintWriter("donors.txt"); if(file.equals(outputPlatinumDonor)) { outputPlatinumDonor.println(name); outputPlatinumDonor.println(addressLine1); outputPlatinumDonor.println(addressLine2); outputPlatinumDonor.println(donationAmount); } else if(file.equals(outputDonor)) { outputDonor.println(name); outputDonor.println(addressLine1); outputDonor.println(addressLine2); outputDonor.println(donationAmount); } outputDonor.close(); outputPlatinumDonor.close(); }
I am not looking for someone to rewrite this for me with the professionally. I honestly want to learn this. My universities tutor isn't in today and I've become a bit frustrated with this problem. I know there are 100 ways to do anything but I would like to keep to my original work as close as possible. For instance I know I am supposed to be buffering my File I/O but my instructor said they are judging us on the effectiveness and not the efficiency at this time.
Any help, hints and advice would be appreciated, thank you very much for your time.