Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java中txt中的数据打印总计列和总计行_Java - Fatal编程技术网

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

使用Java中txt中的数据打印总计列和总计行,java,Java,我是一名初级程序员,我的教授分配的问题如下: 酒店销售人员在文本文件“hotel.txt”中输入2012年的销售额。每行包含以下内容,以空格分隔: 发票ID服务代码发票日期销售金额 invoiceID是一个整数,服务代码是一个从0到4的值,invoiceDate是一个格式为MM/DD/2012的字符串,SaleArmount是一个双精度值。服务代码表示以下内容: 服务代码服务 0住宿 1用餐 2次会议 3次招待会 4水疗中心 编写一个程序,从文件“hotel.txt”读取并打印到文件“hotel

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

酒店销售人员在文本文件“hotel.txt”中输入2012年的销售额。每行包含以下内容,以空格分隔:
发票ID服务代码发票日期销售金额 invoiceID是一个整数,服务代码是一个从0到4的值,invoiceDate是一个格式为MM/DD/2012的字符串,SaleArmount是一个双精度值。服务代码表示以下内容: 服务代码服务 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
publicstaticvoidmain(字符串[]args)引发异常{
Scanner hotelIn=新扫描仪(新文件(“hotelTest.txt”);
PrintWriter hotelOut=新的PrintWriter(新文件(“hotelAnswer.txt”);
double[][]hotelReceipts=新的double[MONTHnames.length][SERVICEnames.length];
double[][]hotelReceiptsTotal=新的double[MONTHnames.length][SERVICEnames.length];
双人房[][]hotelReceiptsMonthlyTotal=新双人房[月长]
[SERVICEnames.length];
while(hotelIn.hasNextLine()){
字符串inputLine=hotelIn.nextLine();
String[]tokens=inputLine.split(“”);
int serviceCode=Integer.parseInt(令牌[1]);
int month=Integer.parseInt(标记[2]。子字符串(0,2));
double amount=double.parseDouble(令牌[3]);
酒店收据【第一个月】【服务代码】+=金额;
酒店收据[第1个月][SERVICEnames.length-1]+=金额;
酒店收据[月长-1][serviceCode]+=金额;
hotelReceiptsTotal[月][服务代码]+=金额;
hotelReceiptsTotal[月][服务名称.长度-1]+=金额;
hotelReceiptsTotal[月长-1][serviceCode]+=金额;
//hotelReceiptsMonthlyTotal[月][服务代码]=总计;
}//一直读到文件结束
System.out.printf(“按月份和服务类型列出的2012年酒店收据总计\n”
+“由Ivy R.Sugars生产\n”
+“日期:2013年11月18日”;
系统输出打印(“|”);

对于(int i=0;i@Andrew汤普森:“一切正常,除了最后一列不是返回总数,而是返回最后一行的最后一个元素。”要更快地获得更好的帮助,请发布一篇文章。