2013-05-03 56 views
0

处理大型程序,从文件读取并打印到另一个文件。它似乎打印出来很好,但我们需要标签。我们的程序打印这样的:Java编程,向表格添加标签和汇总

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"); 
     } 
    } 



} 
+2

你的代码有很多不必要的冗余;你可以简化代码,并且在这个过程中让你更容易调试并且让我们阅读和理解。 – 2013-05-03 23:56:13

+2

问题是什么?因为现在唯一明显的问题是缺少所需格式的代码... – fvu 2013-05-03 23:56:15

+0

您应该使用'Integer.parseInt(...)'来解析'invoiceDate.substring(0,2)'按这个数字然后用它将您的代码减少90%或更多。 – 2013-05-04 00:03:27

回答

0

你想让它看起来像从电子表格打印出来吗?如果是这样,你需要JasperReportshttp://community.jaspersoft.com/project/jasperreports-library

我经常使用iReport设计器,它非常有帮助。

另一种选择可能是输出带有HTML标记的数据以便在浏览器中查看。

如果您只想要TSV文本,只需打印一些标签即可!

out.write("Label 1\tLabel 2\tLabel 3\t ..."); 

要输出个月:

  String[] months; 
      months[0] = "January"; 
      months[1] = "February"; 
      //... 

      for(int i =0;i<=11;i++){ 
       out.print(months[i]+"\t"); 
       for(int j=0;j<=4;j++){ 
        out.print(table[i][j]); 
        out.print("\t"); 
       } 
       out.println(""); 
      } 
+0

我们如何为月份添加标签以及每行和每列的总计。这就是问题所在。 – LClarke27 2013-05-03 23:59:10

+0

@ LClarke27我为你增加了一些答案。 – 2013-05-04 00:03:34

1

你只改变你打印出什么,每个每当你打电话给你的打印命令(的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; 
    } 
} 

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

编辑:

啊,现在你添加表它使更多的意义。您很可能希望在数据循环之后添加一个月(打印新行的位置),并且首先打印列值。