[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Trouble using test driver and also with writing to a text file

Posted on 2010-01-01
10
Medium Priority
?
184 Views
Last Modified: 2013-12-29
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.
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)  
                        { 
                        }

Open in new window

0
Comment
Question by:Indie101
  • 6
  • 4
10 Comments
 

Author Comment

by:Indie101
ID: 26158116
First code is the driver, below is the Report.java how do they work together, as far as I know its just to test the a portion of full program, how does that work, any other info appreciated
import java.text.*;


public class Report

{
//	Instance Variables (all private)


	private String[] _departments; //A String array containing the names of four departments
	private double[] _grossTotals; //An array of doubles to hold gross totals by dept
	private double[] _taxTotals; //An array of doubles to hold tax totals by dept


//	constructor - Create two total arrays and initialise them to zero

	public Report()
	{

		_departments = new String[4]; 
		_departments[0] = "Accounting";
		_departments[1] = "Sales";
	    _departments[2] = "HR";
	    _departments[3] = "Administration";
		
		_grossTotals = new double[]{0,0,0,0};
		_taxTotals = new double[]{0,0,0,0};
	}

	public void addToTotals(double gr, double tx, char dept)
	{
		//first recover department code
		int dcode =  (int) dept - (int) '1';

		//now use numeric department code to index into array value to modify
		this._grossTotals[dcode] = this._grossTotals[dcode] + gr;
		this._taxTotals[dcode] = this._taxTotals[dcode] + tx;
	}

	public void printTotals()
	{
		//Purpose:  To print  a report showing dept name, gross totals and tax totals for each department 
NumberFormat nf = NumberFormat.getInstance(); 
 
System.out.println("Department Bank Report"); 
for(int j = 0; j<4; j++) { 
  System.out.println("Department" +"\t" + "Total" + "\t" + "Tax Paid"); 
  System.out.println(_departments[j] + "\t" + nf.format(_grossTotals[j]) + "\t" + nf.format(_taxTotals[j])); 
}

	}

	}

Open in new window

0
 

Author Comment

by:Indie101
ID: 26158123
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();
	}
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26158399
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..?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Indie101
ID: 26158417
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!! :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26158442
If it's working (you said it was), first confirm that my first comment does what you want
0
 

Author Comment

by:Indie101
ID: 26158522
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26158528
I posted the command you need to issue at the command line. That's all you need
0
 

Author Comment

by:Indie101
ID: 26158581
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...
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 26158856
You've already been given code for writing to a file, for which you've awarded point. If you have any further questions, you must post them in the original question. This one should be closed
0
 

Author Closing Comment

by:Indie101
ID: 31671783
Answer while accurate was not what was looking for
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question