Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: File I/O reading from text and writing to another, PLEASE ADVISE! basic

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:
    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.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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:

    PrintWriter outputPlatinumDonor = new PrintWriter("PlatinumDonors.txt");
    PrintWriter outputDonor = new PrintWriter("Donors.txt");

    And then the ones you create in writeToFile:

    PrintWriter outputPlatinumDonor = new PrintWriter("PlatinumDonors.txt");
    PrintWriter outputDonor = new PrintWriter("donors.txt");

    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: File I/O reading from text and writing to another, PLEASE ADVISE! basic

    {
    		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

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  11. #11
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.
    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();

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  15. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Loki617 (April 16th, 2012)

Similar Threads

  1. Trouble with writing/reading array to/from file
    By MaximusPrime in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2012, 08:41 PM
  2. Beginner I/O Help: Writing To a Text File At the End of Existing Lines of Text
    By BloomingNutria in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 28th, 2012, 03:03 PM
  3. file reading & writing
    By macko in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 12th, 2011, 08:54 AM
  4. Reading a writing binary file
    By stu1811 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: July 30th, 2010, 12:58 PM
  5. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:16 PM