Indie101
asked on
Trouble using test driver and also with writing to a text file
FIrst of all have a slight issue with the driver program, how is it picked up when used.
Having problems writing this to a file as part of payroll problemQuestion: Total java noob, got a lot of help regarding decimal formatting for following code,kudos to the site, working on a payroll project as part of a team, have been trying to write this report to a file, any help appreciated.. Have tried to use
http://java.sun.com/docs/books/tutorial/essential/io/file.html for info...Cant seem to see how it works
From what I was told filewriter can be used as well, which is best and if possible could a working example both ways be constructed...I want to learn but also want to see some working examples before I modify it, even if both working examples were done for a sample which i could learn from that would be great.
Having problems writing this to a file as part of payroll problemQuestion: Total java noob, got a lot of help regarding decimal formatting for following code,kudos to the site, working on a payroll project as part of a team, have been trying to write this report to a file, any help appreciated.. Have tried to use
http://java.sun.com/docs/books/tutorial/essential/io/file.html for info...Cant seem to see how it works
From what I was told filewriter can be used as well, which is best and if possible could a working example both ways be constructed...I want to learn but also want to see some working examples before I modify it, even if both working examples were done for a sample which i could learn from that would be great.
public static void setContents(File aFile, String aContents)
throws FileNotFoundException, IOException
{
//System.out.println("File contents: " +aContents);
logger.debug("File contents: "+ aContents);
//if the file is null, then IllegalArgumentException is raised
if (aFile == null)
{
throw new IllegalArgumentException(EMFILESHOULDNOTBENULL);
}
//if the file is a directory, then IllegalArgumentException is raised
if (!aFile.isFile())
{
throw new IllegalArgumentException(EMSHOULDNOTBEDIR + aFile);
}
//if the file is a read-only, then IllegalArgumentException is raised
if (!aFile.canWrite())
{
throw new IllegalArgumentException(EMFILEREADONLY + aFile);
}
//use buffering
Writer output = new BufferedWriter(new FileWriter(aFile));
try
{
//FileWriter always assumes default encoding is OK!
output.write( aContents );
}
catch (IOException e)
{
}
finally
{
try
{
//closing the I/O connection
output.close();
}
catch (Exception e)
{
}
ASKER
Apologies first snippet of code is towards writing file to text, (not sure how to use it) this is the driver program
\\Report test class Bill Fitzgerald
{
}
import java.util.*;
public class MainClass
{
public static void main(String[] args)
{
Report newReport = new Report();
//apply constructor to set initial values
newReport.addToTotals(100, 10, '1');
newReport.addToTotals(100, 10, '1');
newReport.addToTotals(100, 10, '1');
newReport.addToTotals(100, 10, '1');
newReport.addToTotals(150, 15, '2');
newReport.addToTotals(150, 15, '2');
newReport.addToTotals(150, 15, '2');
newReport.printTotals();
}
}
Indie101, you've already accepted an answer to this question (having completely ignored my comments) so i'm wondering why you're posting it again..?
ASKER
I accepted previous question in relation to the number formatting, sorry If i ignored your comment, I just hadnt come across it before, TBH I just wanted a working example of the above...If you could kindly help me again CEHJ would appreciate it a lot... You mentioned before
"The easiest way to print to file is just to redirect the output (see below)
If you're interested in formatting in a table with correct widths and alignment, use System.out.printf
1:
java MainClass >sales.log
"
Could you setup for my report class, I am trying to revise as I go along, just finding it hard!! :-)
"The easiest way to print to file is just to redirect the output (see below)
If you're interested in formatting in a table with correct widths and alignment, use System.out.printf
1:
java MainClass >sales.log
"
Could you setup for my report class, I am trying to revise as I go along, just finding it hard!! :-)
If it's working (you said it was), first confirm that my first comment does what you want
ASKER
The comment that was made by another expert worked in relation to decimal formatting. As I have said I wasnt aware of how to use your comment can you show me how it works for the report class.
I posted the command you need to issue at the command line. That's all you need
ASKER
java MainClass >sales.log
Thanks for explaining above, :-) I wasnt aware initially it was for cmd line.
I can't use it at command line though has to be using fileoutputstream or filewriter. Sorry about any confusion earlier..
Can you provide any help with fileoutputstream or filewriter CEHJ?? :-)
Would be great if you can...
Thanks for explaining above, :-) I wasnt aware initially it was for cmd line.
I can't use it at command line though has to be using fileoutputstream or filewriter. Sorry about any confusion earlier..
Can you provide any help with fileoutputstream or filewriter CEHJ?? :-)
Would be great if you can...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Answer while accurate was not what was looking for
ASKER
Open in new window