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 3 of 3

Thread: Writing output from a void method to a file

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Writing output from a void method to a file

    I have two classes.

    Amortization class has the following void method.

        public void displayResults()
        {
            System.out.println("The loan amount is $" + loanAmt);
            System.out.println("The number of payments per year is " + pmtsPerYear);
            System.out.println("The number of years is " + numYears);
            System.out.println("The interest rate is " + annIntRate + "%");
            System.out.println("The amount of the loan payments will be $" + this.getPmtAmt() + ".");
     
        }


    The second class called AmortizationClient has my main method.


    PROBLEM:
    I'm trying to figure out how to get the output from the void method to write to a newly created file. I know how to use an object to invoke this void method....what I don't know is how to do it when using the PrintWriter Class. This is all being done within a switch statement.


    case 8:
    					System.out.println();
    					System.out.println("Enter the name of the file you wish to save the parameters to: ");
    					fileName = keyboard.nextLine();
    					PrintWriter outputStream = null;
    					try
    					{
    						outputStream = new PrintWriter(fileName);
    					}
    					catch(FileNotFoundException e)
    					{
    						System.out.println("File is not found.");
    						System.exit(0);
    					}
     
    					outputStream.print();
     
     
    					outputStream.close();
    					System.out.println("The files were done.");
    					break;



    Thanks!!!


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Writing output from a void method to a file

    I'm trying to figure out how to get the output from the void method to write to a newly created file.
    What type output you want to get from your method? If it's File, return File, if it's String, return String.
    Remember the syntax;
    AccessSpecifier ReturnType MethodName(Arguments...){
    Body of Function
    }
    Or 
    public String methodName(int a, String b){
    blah blah;
    }

  3. #3
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Writing output from a void method to a file

    Quote Originally Posted by TheWhopper858 View Post
    .what I don't know is how to do it when using the PrintWriter Class. This is all being done within a switch statement.
    PrintWriter pw = new PrintWriter();
    pw.print("Hello");
    pw.println("Goodbye");

    Spring 3
    Last edited by cafeteria84; January 22nd, 2012 at 04:02 PM.

Similar Threads

  1. So Lost with void method
    By tripline in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 27th, 2011, 02:45 PM
  2. Replies: 1
    Last Post: April 7th, 2011, 02:32 PM
  3. Reverse character using void method
    By bgwilf in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 8th, 2010, 07:25 AM
  4. Writing Output To New File
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 6th, 2010, 07:25 PM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM