File I/O reading from text and writing to another, PLEASE ADVISE! basic
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:
Code Java:
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)
Code Java:
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.
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
Have you stepped through this with a debugger, or at least added some print statements to figure out where the failure point is? For example, does your code enter the writeToFile() method? Does it enter those if statements? Why are you checking whether an instance you create inside the writeToFile() method is equal to an instance you create inside the main() method? That's not going to work, since they aren't the same instance.
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
I used the print method in a few places. The problem seems to occur after the first four lines of the donors.txt has run through. Reading the data in doesn't seem to be the problem. Which instance are you referring to? Where should the instance be created.
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
The instructions for the writetofile method.
"writeToFile" method that accepts an open PrintWriter object, and the name and address of the donor. If the donor donated more than $1,000, the PrintWriter object passed to this method will be pointing to PlatinumDonors.txt. Otherwise, the PrintWriter object passed will be pointing to Donors.txt.
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
The instances I'm referring to are the ones you create in main:
And then the ones you create in writeToFile:
You then pass the instances from main into writeToFile, as a parameter named file (strange choice of variable name, but that's beside the point).
Then you compare the file instance (which you created in main) to the instances you created in writeToFile, in your if statement. I would bet that neither if statement ever evaluates to true, since the instances from main and the instances in writeToFile have nothing to do with each other.
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
Yea, I was getting error messages without the second PrintWriter instance in my writeToFile Method, that is why I did that. Thank you for your help, I guess I should keep the PrintWriter method in the main only?
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
I'm not really sure what you mean by PrintWriter method. But judging from your assignment, you aren't supposed to be opening a PrintWriter inside the writeToFile method, you're supposed to pass one in as an argument.
But I think your central confusion lies in how you're thinking about instances. Think of an Object as a type of Animal, and an instance as a particular Animal's name. So think of a PrintWriter as a Cat, and each time you get a new Cat, you give it a name (just like you give PrintWriters a file to write to).
So in your main method, you have two Cats, let's call them Stanley and Spot. So far so good. Now in your printToFile you have two OTHER Cats, who happen to also be named Stanley and Spot. Does that mean that the first Stanley is the same as the second Stanley? No! So comparing them like you do doesn't make a ton of sense.
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
Code Java:
{
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();
PrintWriter file = null;
if(donationAmount >= 1000)
{
file = outputPlatinumDonor;
}
else if(donationAmount < 1000)
{
file = outputDonor;
}
athritis.writeToFile(name, addressLine1, addressLine2, donationAmount, file);
}
input.close();
// and the method
public void writeToFile(String name, String addressLine1, String addressLine2, double donationAmount, PrintWriter file) throws IOException
{
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 took the PrintWriter out of my writeToFile method. Java isn't recognizing my cat's in the class method now. This is why I originally made the mistake of putting it in the method. Thank you again
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
Well now you're still comparing your Cat (haha I love that analogy) to another Cat, but the second Cat doesn't exist!
I think you're overthinking your writeToFile() method. Why do you need an if statement inside of it at all? Don't you already decide which file to write to before you call the method?
Your method is not supposed to decide which Cat to pet, or compare a Cat to another Cat. It's supposed to take a single Cat and pet it. It's up to the other parts of the program to figure out which Cat should be pet.
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
Well my input data is a list of donations made by people. So if donated alot I'm supposed to pet Stanley and if they donated a little I'm supposed to pet Spot. I have to read this information and decide which one to pet, or which document to write the information to. There has to be two documents in which the information ends up. I can't get printwriter to do anything for me.
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
Ok, this is where I am now, I feel like I'm getting somewhere but it still doesn't write any information.
Code Java:
public void writeToFile(String name, String addressLine1, String addressLine2, double donationAmount, PrintWriter in) throws IOException
{
in.println(name);
in.println(addressLine1);
in.println(addressLine2);
in.println(donationAmount);
}
// main
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();
if(donationAmount >= 1000)
{
athritis.writeToFile(name, addressLine1, addressLine2, donationAmount, outputPlatinumDonor);
}
else if(donationAmount < 1000)
{
athritis.writeToFile(name, addressLine1, addressLine2, donationAmount, outputDonor);
}
}
input.close();
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
That's true, but that doesn't mean you should do anything other than writing to a file from your writeToFile method. Think about it this way: you have a bag of treats, and each treat is either catnip or a mouse. Stanley likes catnip, and Spot likes mice. You have to go through the bag and give each treat to the correct cat.
Code java:
public class CatTest{
public static void main(){
//Stanley likes catnip
Cat stanley = new Cat("Stanley");
//Spot likes mice
Cat spot = new Cat("Spot");
for(Treat t : treats){
if(t == catnip){
giveTreat(stanley, t);
}
else if(t == mouse){
giveTreat(spot, t);
}
}
}
public static void giveTreat(Cat c, Treat t){
c.giveTreat(t);
}
}
Your giveTreat method doesn't do anything other than give the supplied Cat the treat. You don't care which Cat it is or what kind of Treat it is.
This is a ridiculous example, but hopefully it helps.
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
I didn't see your last post until after I posted my last Cat example.
Take a look at the Scanner API, specifically the hasNext() method: Scanner (Java Platform SE 6)
Also, this is worth a read: http://www.javaprogrammingforums.com...t-println.html
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
Your examples helped explain the principle but I still cannot resolve why my Donors.txt and PlatinumDonors.txt files remain empty and void of any information. Thanks again
Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic
I think we're crossing each other's posts, so I'll just recommend again that you check out the links in my previous post. You aren' using the hasNext() method correctly, so consult the API to see how it's used. If that doesn't work, then use a debugger or print statements to narrow it down to why.