Writing output from a void method to a file
I have two classes.
Amortization class has the following void method.
Code :
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.
Code :
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!!!
Re: Writing output from a void method to a file
Quote:
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;
Code :
AccessSpecifier ReturnType MethodName(Arguments...){
Body of Function
}
Or
public String methodName(int a, String b){
blah blah;
}
Re: Writing output from a void method to a file
Quote:
Originally Posted by
TheWhopper858
.what I don't know is how to do it when using the PrintWriter Class. This is all being done within a switch statement.
Code :
PrintWriter pw = new PrintWriter();
pw.print("Hello");
pw.println("Goodbye");
Spring 3