这是一个大学课程作业,由TotalSales和TotalSalesTest类组成。在主程序中,我创建了一个二维数组,用于输出一个包含4行和5列交叉总数的列式布局。该程序为每个销售人员(1 - 4)逐行输出销售总额,并按产品(1-5)的列输出。我在数组中创建了额外的元素来存储行和列的总数。到目前为止,两个类都编译。问题是虽然PrintWriter创建了一个记事本文件,但它不会打印到它。我可以在这个问题上使用一些帮助。这是代码`
//write program in a two diminsional array to output a columnar layout with cross-totals in 4 rows and 5 columns
//program outputs sales totals by row for each sales person(1 - 4) and output by column for products(1 - 5)
//create extra elements to store total for rows and columns
import java.util.Scanner;
import java.io.*;
public class TotalSales
{
private int salesPerson; //declare class variable
private int productNumber;//declare class variable
private double totalSales;//declare class variable
private double allSales;
//declare input and output variables
Scanner inFile; //declare inFile variable
PrintWriter outFile;//declare outFile variable
double[][]sales = new double[6][7];//declare array sales
public void initializer()
{
try
{
inFile = new Scanner( new File( "assign06.txt" ) );
outFile = new PrintWriter( "MonthlyTotalSales.txt" );
outFile.flush();
}
catch (FileNotFoundException e)
{
System.out.println("The input file could not be found!");
System.exit(1);
}
while(inFile.hasNext()) //while there is data to process…
{
salesPerson = inFile.nextInt();//reads salesPerson
productNumber = inFile.nextInt();//reads productNumber
totalSales = inFile.nextDouble();//reads totalSales
sales[salesPerson][productNumber]+=totalSales;
sales[salesPerson][6]+=totalSales;
sales[5][productNumber]+=totalSales;
allSales += totalSales;
} //end while loop
printDetails(sales);//call method printDetails
finishUp();//call method finishUp
}//end initializer
public void printDetails(double[][] array)
{
outFile.println("\t1\t2\t3\t4\t5");
for (int salesPerson =1; salesPerson <5; salesPerson++)
{
outFile.print(salesPerson+ " ");
for(int productNumber=1; productNumber <=array.length; productNumber++)
outFile.print(array[salesPerson][productNumber]+" ");
//end inside loop
outFile.println();
}//end outside loop
outFile.print("Total: \t ");
for(int salesTotal=1; salesTotal<array.length; salesTotal++)
{
outFile.print(array[5][salesTotal] +" ");
}
outFile.print(allSales);
outFile.println();
outFile.print(" ");
outFile.println();
}//end printDetails
public void finishUp()
{
inFile.close();
outFile.close();
System.out.println("The program has finished.");
}//end finishUp
}//end class TotalSales
以下是测试程序:
public class TotalSalesTest
{
public static void main(String[] args)
{
TotalSales ts = new TotalSales();
ts.initializer();
}//end method main
}
以下是输入的文本文件:
1 1 37.50
1 2 77.00
1 3 68.75
1 4 61.25
1 5 175.00
2 1 45.00
2 2 66.00
2 3 27.50
2 4 49.00
2 5 250.00
3 1 67.50
3 2 33.00
3 4 73.50
3 5 200.00
4 1 15.00
4 2 99.00
4 3 123.75
4 4 85.75
4 5 125.00
1 1 60.00
1 2 88.00
1 3 41.25
1 4 49.00
1 5 225.00
2 1 67.50
2 2 33.00
2 3 27.50
2 4 122.50
2 5 25.00
3 1 60.00
3 2 44.00
3 3 96.25
3 4 36.75
3 5 50.00
4 1 75.00
4 2 11.00
4 3 41.25
4 4 98.00
4 5 125.00
1 1 45.00
1 2 33.00
1 3 27.50
1 4 61.25
1 5 200.00
2 1 52.50
2 2 22.00
2 3 13.75
2 4 36.75
2 5 50.00
3 1 37.50
3 2 88.00
3 3 96.25
3 4 36.75
4 1 37.50
4 2 77.00
4 3 82.50
4 4 73.50
4 5 25.00
1 1 30.00
1 2 88.00
1 3 41.25
1 4 12.25
1 5 175.00
2 1 45.00
2 2 22.00
2 3 68.75
2 4 98.00
3 2 88.00
3 3 41.25
3 4 24.50
4 1 30.00
4 2 88.00
4 3 82.50
4 4 122.50
4 5 175.00
答案 0 :(得分:0)
在初始化作者之前致电printDetails
...在printDetails
之后移动outFile = new PrintWriter( "MonthlyTotalSales.txt" );
来电,应该没问题。但是,你应该包含你的构造函数,因为它现在是,我认为你的代码抛出NullPointerException
,因为编写器从未被初始化。您还需要在写入之前关闭文件,或者至少需要刷新。
你的代码的整个结构都很糟糕,你不应该在类上有mainProgram
这样的方法,你不应该只使用文件作为属性来在每个方法中使用它,你永远不应该在不同方法中分离创建和关闭i / o类。
答案 1 :(得分:0)
似乎你没有调用outFile.close();
完成文件写入后,请调用finishUp()方法。
答案 2 :(得分:0)
我认为您的程序在
上使用nullpointer崩溃了inFile.hasNext()
因为你从未实际打开过甚至实例化输入文件以及try / catch块中的输出文件。糟糕!
变化
try
{
outFile = new PrintWriter( "MonthlyTotalSales.txt" );
}
到
try
{
inFile = new Scanner(new File("Input.txt"));
outFile = new PrintWriter( "MonthlyTotalSales.txt" );
}