Java 编程,向表添加标签和总计

标签 java arrays label

正在开发大型程序,从一个文件读取并打印到另一个文件。看起来打印效果很好,但我们需要标签。我们的程序打印如下:

40851   50497   813082  1118366 1120629 
34400   45547   824439  1425116 1429306 
39249   48833   809627  1101561 1103908 
57939   67308   821564  1126250 1128620 
64000   76037   827086  1424632 1431210 
34200   40240   554391  767810  772107  
89310   101149  2351871 2971044 2980458 
84370   95851   2470295 3096137 3105516 
59700   71190   2290032 2905223 2909869 
67600   79212   2419095 3031704 3034002 
60200   71594   2348818 2969848 2972035 
46000   56050   824092  1300904 1303163 

我们需要它来打印总数和标签,如下所示(忽略数字):

enter image description here

这是我们的代码:

package hotelreport;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.Scanner;

public class HotelReport {

    static File hotelFile = new File("store.txt");
    static Path outputFilePath = Paths.get("storeAnswer.txt");
    static int  invoiceID;
    static int serviceCode;
    static String invoiceDate;
    static double saleAmount;
    static int rows = 13;
    static int columns = 6;
    static int[][] table;

    public static void main(String[]args){

            getData();
            PrintWriter out;
            File file = new File("storeAnswer.txt");

            try {
                out = new PrintWriter("storeAnswer.txt");
                for(int i =0;i<=11;i++){
                    for(int j=0;j<=4;j++){
                        out.print(table[i][j]);
                        out.print("\t");
                    }
                    out.println("");
                }
                out.close();
            } catch (FileNotFoundException e) {
                System.out.println("File not found");
            }



    }

    public static void getData(){
        table = new int[rows][columns];

        try {
            Scanner sc = new Scanner(hotelFile);

            while(sc.hasNext()){
                invoiceID = sc.nextInt();
                serviceCode = sc.nextInt();
                String invoiceDate = sc.next();
                saleAmount = sc.nextDouble();
                switch(serviceCode){
                    case 0: 
                        if(invoiceDate.startsWith("01")){
                            table[0][0]+=saleAmount;
                        }else if(invoiceDate.startsWith("02")){
                            table[1][0]+=saleAmount;
                        }else if(invoiceDate.startsWith("03")){
                            table[2][0]+=saleAmount;
                        }else if(invoiceDate.startsWith("04")){
                            table[3][0]+=saleAmount;
                        }else if(invoiceDate.startsWith("05")){
                            table[4][0]+=saleAmount;
                        }else if(invoiceDate.startsWith("06")){
                            table[5][0]+=saleAmount;
                        }else if(invoiceDate.startsWith("07")){
                            table[6][0]+=saleAmount;
                        }else if(invoiceDate.startsWith("08")){
                            table[7][0]+=saleAmount;
                        }else if(invoiceDate.startsWith("09")){
                            table[8][0]+=saleAmount;
                        }else if(invoiceDate.startsWith("10")){
                            table[9][0]+=saleAmount;
                        }else if(invoiceDate.startsWith("11")){
                            table[10][0]+=saleAmount;
                        }else if(invoiceDate.startsWith("12")){
                            table[11][0]+=saleAmount;
                        };
                    case 1:
                        if(invoiceDate.startsWith("01")){
                            table[0][1]+=saleAmount;
                        }else if(invoiceDate.startsWith("02")){
                            table[1][1]+=saleAmount;
                        }else if(invoiceDate.startsWith("03")){
                            table[2][1]+=saleAmount;
                        }else if(invoiceDate.startsWith("04")){
                            table[3][1]+=saleAmount;
                        }else if(invoiceDate.startsWith("05")){
                            table[4][1]+=saleAmount;
                        }else if(invoiceDate.startsWith("06")){
                            table[5][1]+=saleAmount;
                        }else if(invoiceDate.startsWith("07")){
                            table[6][1]+=saleAmount;
                        }else if(invoiceDate.startsWith("08")){
                            table[7][1]+=saleAmount;
                        }else if(invoiceDate.startsWith("09")){
                            table[8][1]+=saleAmount;
                        }else if(invoiceDate.startsWith("10")){
                            table[9][1]+=saleAmount;
                        }else if(invoiceDate.startsWith("11")){
                            table[10][1]+=saleAmount;
                        }else if(invoiceDate.startsWith("12")){
                            table[11][1]+=saleAmount;
                        };
                    case 2:
                        if(invoiceDate.startsWith("01")){
                            table[0][2]+=saleAmount;
                        }else if(invoiceDate.startsWith("02")){
                            table[1][2]+=saleAmount;
                        }else if(invoiceDate.startsWith("03")){
                            table[2][2]+=saleAmount;
                        }else if(invoiceDate.startsWith("04")){
                            table[3][2]+=saleAmount;
                        }else if(invoiceDate.startsWith("05")){
                            table[4][2]+=saleAmount;
                        }else if(invoiceDate.startsWith("06")){
                            table[5][2]+=saleAmount;
                        }else if(invoiceDate.startsWith("07")){
                            table[6][2]+=saleAmount;
                        }else if(invoiceDate.startsWith("08")){
                            table[7][2]+=saleAmount;
                        }else if(invoiceDate.startsWith("09")){
                            table[8][2]+=saleAmount;
                        }else if(invoiceDate.startsWith("10")){
                            table[9][2]+=saleAmount;
                        }else if(invoiceDate.startsWith("11")){
                            table[10][2]+=saleAmount;
                        }else if(invoiceDate.startsWith("12")){
                            table[11][2]+=saleAmount;
                        };
                    case 3:
                        if(invoiceDate.startsWith("01")){
                            table[0][3]+=saleAmount;
                        }else if(invoiceDate.startsWith("02")){
                            table[1][3]+=saleAmount;
                        }else if(invoiceDate.startsWith("03")){
                            table[2][3]+=saleAmount;
                        }else if(invoiceDate.startsWith("04")){
                            table[3][3]+=saleAmount;
                        }else if(invoiceDate.startsWith("05")){
                            table[4][3]+=saleAmount;
                        }else if(invoiceDate.startsWith("06")){
                            table[5][3]+=saleAmount;
                        }else if(invoiceDate.startsWith("07")){
                            table[6][3]+=saleAmount;
                        }else if(invoiceDate.startsWith("08")){
                            table[7][3]+=saleAmount;
                        }else if(invoiceDate.startsWith("09")){
                            table[8][3]+=saleAmount;
                        }else if(invoiceDate.startsWith("10")){
                            table[9][3]+=saleAmount;
                        }else if(invoiceDate.startsWith("11")){
                            table[10][3]+=saleAmount;
                        }else if(invoiceDate.startsWith("12")){
                            table[11][3]+=saleAmount;
                        };
                    case 4:
                        if(invoiceDate.startsWith("01")){
                            table[0][4]+=saleAmount;
                        }else if(invoiceDate.startsWith("02")){
                            table[1][4]+=saleAmount;
                        }else if(invoiceDate.startsWith("03")){
                            table[2][4]+=saleAmount;
                        }else if(invoiceDate.startsWith("04")){
                            table[3][4]+=saleAmount;
                        }else if(invoiceDate.startsWith("05")){
                            table[4][4]+=saleAmount;
                        }else if(invoiceDate.startsWith("06")){
                            table[5][4]+=saleAmount;
                        }else if(invoiceDate.startsWith("07")){
                            table[6][4]+=saleAmount;
                        }else if(invoiceDate.startsWith("08")){
                            table[7][4]+=saleAmount;
                        }else if(invoiceDate.startsWith("09")){
                            table[8][4]+=saleAmount;
                        }else if(invoiceDate.startsWith("10")){
                            table[9][4]+=saleAmount;
                        }else if(invoiceDate.startsWith("11")){
                            table[10][4]+=saleAmount;
                        }else if(invoiceDate.startsWith("12")){
                            table[11][4]+=saleAmount;
                        };


                }
            }

        } catch (FileNotFoundException e) {
            System.out.println("File not found. Check Location");
        } catch (IOException e1) {
            System.out.println("File coud not be created");
        }
    }



}

最佳答案

每当您调用打印命令(out.print)时,您只需更改每个值打印的内容

不相关,但如果你正在这样做:

                    if(invoiceDate.startsWith("01")){
                        table[0][0]+=saleAmount;
                    }else if(invoiceDate.startsWith("02")){
                        table[1][0]+=saleAmount;
                    }else if(invoiceDate.startsWith("03")){
                        table[2][0]+=saleAmount;
                    }else if(invoiceDate.startsWith("04")){
                        table[3][0]+=saleAmount;
                    }else ...

为什么不直接使用循环?:

for (int i=0; i<12; i++) {
    if (invoiceDate.startsWith("0" + (i + 1)) {
        table[i][0]+=saleAmount;
    }
}

当然,仍然可以进行更多改进,但这至少是一个开始。

编辑:

啊,现在添加表格就更有意义了。您很可能希望在数据循环后添加月份(在其中打印新行),并初始打印列值。

关于Java 编程,向表添加标签和总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16369163/

相关文章:

java - 计算两个列表之间的重复值时如何短路?

python - Kivy 标签左对齐和调整大小

css - 将 CSS 属性添加到 Symfony 表单标签?

java - 适当使用Java中的Multi-level Break

java - 考虑在您的配置中定义类型为 'com.gisapp.gisapp.dao.IUserDAO' 的 bean

java - 迭代嵌套的复杂 json 对象

java - 如何在不知道数组大小的情况下初始化数组?

iphone - 指针、NSMutableArray、保留、循环和困惑

c++ - 有没有更好的方法可以遍历数组中的每个元素而不使用索引的for循环?

java - 无法实例化类型 SocketChannel