使用Java中的txt数据打印总列和总行

时间:2013-11-23 16:59:15

标签: java

我是一名初学程序员,我的教授已将问题分配如下:

酒店销售人员在文本文件“hotel.txt”中输入2012年的销售额。每行包含以下空格分隔的以下内容:
invoiceID serviceCode invoiceDate saleAmount invoiceID是一个整数,服务代码是0到4之间的值,invoiceDate是MM / DD / 2012格式的字符串,而saleAmount是double。服务代码代表以下内容: 服务代码服务 0住宿 1餐饮 2次会议 3招待会 4个水疗中心

编写一个从文件“hotel.txt”读取的程序并打印到文件“hotelAnswer.txt”。该报告应与下面的报告类似。用你的名字替换我的名字。请注意,该示例用于演示用于报表的格式。显示的值不是考试数据的实际总数。创建自己的数据文件以测试您的程序。在单独的文件(使用记事本,写字板或Microsoft Word)中,说明创建数据文件的基本原理。在您提交给Moodle的提交中包括:(1)您的程序,(2)您的测试文件,(3)测试文件的基本原理,以及(4)您的结果文件。

我编写了代码并且一切正常,除了,最后一列没有返回总数,它返回最后一行的最后一个元素。以下是我到目前为止的情况:

public static void main(String[] args)throws Exception {
    Scanner hotelIn = new Scanner(new File("hotelTest.txt"));
    PrintWriter hotelOut = new PrintWriter(new File("hotelAnswer.txt"));
    double[][] hotelReceipts = new double[MONTHnames.length][SERVICEnames.length];
    double[][] hotelReceiptsTotal = new double[MONTHnames.length][SERVICEnames.length];
    double[][] hotelReceiptsMonthlyTotal = new double[MONTHnames.length]           
                                                            [SERVICEnames.length];


    while(hotelIn.hasNextLine()){
        String inputLine = hotelIn.nextLine();
        String[] tokens = inputLine.split(" ");
        int serviceCode = Integer.parseInt(tokens[1]);
        int month = Integer.parseInt(tokens[2].substring(0,2));
        double amount = Double.parseDouble(tokens[3]);
        hotelReceipts[month-1][serviceCode] += amount;
        hotelReceipts[month-1][SERVICEnames.length-1] += amount;
        hotelReceipts[MONTHnames.length-1][serviceCode] += amount;
        hotelReceiptsTotal[month][serviceCode]+= amount;
        hotelReceiptsTotal[month][SERVICEnames.length-1] += amount;
        hotelReceiptsTotal[MONTHnames.length-1][serviceCode] += amount;

        //hotelReceiptsMonthlyTotal[month][serviceCode] = total;
    }//read until end of file
    System.out.printf("   Hotel 2012 Receipt Totals by Month and Service Type\n"
            + "            Produced by Ivy R. Sugars\n              "
            + "Date: November 18, 2013\n\n");
    System.out.print("         |");

    for(int i=0;i<SERVICEnames.length;i++){
        System.out.printf("%15s ", SERVICEnames[i]); 
    }

    System.out.println();
    System.out.println("_________|__________________________________________"
            + "__________________________________________________________");
    for(int month=0;month<MONTHnames.length-1;month++){
        System.out.printf("%9s| ", MONTHnames[month]);
        for(int i=0;i<SERVICEnames.length;i++){
            System.out.printf("%15.2f ", hotelReceipts[month][i]);
         }//columns
        System.out.println();
    }//rows
    System.out.println("_________|__________________________________________"
            + "___________________________________________________________");
    for (int month = 12; month < MONTHnames.length; month++) {
        System.out.printf("%9s| ", MONTHnames[month]);
        for (int i = 0; i < SERVICEnames.length; i++) {
            System.out.printf("%15.2f ", hotelReceiptsTotal[month][i]);
        }//columns
    }//rows

    hotelOut.close();
   }//main
}//MyHotel

有人请帮忙!

0 个答案:

没有答案