2012-01-12 5 views
0

txtファイルを2次元配列に変換しましたが、どのように整理するといいですか?2d配列を整理テーブルの表記に変換する

私の入力ファイルは次のようになります。

 
[Name], Exam1, Exam2, Exam3 
John, 99, 88, 89 
May, 99, 100, 100 
Mary, 100, 100, 100 
Peter, 60, 60, 60 

現在、私は得る:

 
[Name] Exam1 Exam2 Exam3 
John 99 88 89 
May 99 100 100 
Mary 100 100 100 
Peter 60 60 60 

私はデータは、私はそれをどのように行うことができ、より読みやすいテーブルのように見えるしたいですか?

コード:

public static void main(String[] args) throws Exception{ 
    File file = new File("test.txt"); 
    BufferedReader br = new BufferedReader(new FileReader(file)); 
    int width = 0, height = 0; 
    String line = ""; 

    /*Find number of row and column of the file.*/ 
    while ((line = br.readLine()) != null) 
    { 
       if (width == 0) 
       { 
        /*Find the number of row using split method(",")*/ 
        String[] str = line.split(","); 
        width = str.length; 
       } 
     height++; 
    } 
    System.out.println("Row : " + height); 
    System.out.println("Column : " + width); 

    /*Adding values to the 2D Array.*/ 
    String[][] data = new String[height][width]; 
    br = new BufferedReader(new FileReader(file)); 

    for (int i = 0; i < height; i++) 
    { 
     if ((line = br.readLine()) != null) 
     { 
      for (int j = 0; j < width; j++) 
      {         
       String[] str = line.split(",");  
       data[i][j] = str[j]; 

       System.out.print(data[i][j] + " "); 
      } 

     } 

     System.out.println(""); 

    } 
} 

はどうもありがとうございました。 printfの

またはあなたが以下のようにしている、このような方法でそれを行うことができフォーマッタ

答えて

1

トライ出力:

// To Store and Show Values on the screen. 
    int columnWidth = 15; 

    for (int i = 0; i < height; i++) 
    { 
     if ((line = br.readLine()) != null) 
     { 
      // After reading, we are seperating them. 
      String[] str = line.split(", "); 
      for (int j = 0; j < width; j++) 
      {               
       data[i][j] = str[j]; 
       // Here we are making the word length of each String equal, 
       // i.e. equal to column width. So that each String value comes 
       // below each other. Increase the value of columnWidth variable 
       // if you want to increase the width of a column or vice versa. 
       System.out.format("%-" + columnWidth + "s", data[i][j]); 
      } 
     } 
     System.out.print("\n"); 
    } 

を助けるかもしれない希望。

よろしく

+0

感謝。私はjavaに新しいです、どこでどのようにprintf/Formatterを使用するのですか?どうもありがとう。ここ –

+0

全く問題 は、APIフォーマッタへのリンクではありません:printfのためとしてhttp://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html 、私はしないでくださいapiが使い方について話しすぎると思う。ここに例があります:http://www.java2s.com/Code/JavaAPI/java.lang/SystemoutprintfsssnStringstr1Stringstr2Stringstr3.htm もしあなたがJavaを使い慣れていないなら、あなたの親友はapiドキュメントとソースコードです。 :) – ligerdave

0

を使用することができます使用して

+0

ありがとう、それは動作します。しかし、言葉が長すぎると、間違って見えるものよりも大きなギャップを残すことがあります。どうすれば修正できますか?どうもありがとう。 –

+0

@BensonLeung:列内の値を調整するために、好きなようにcolumnWidthの値を増減します。列の幅を広げたい場合はそれを大きくし、幅を狭くしたい場合は小さくします。よろしくお願いします。 –

+0

@BensonLeung:今私は必要な変更を行っています。よろしく –

0

私はあなたがあなたの製品ですべてのことのSystem.outのものをするつもりはないが、私はあなたが行う必要があります基本的な手順を示すために、あなたの上に構築し、一例ではそれを使用するつもりだと想像。

基本的にデータを2回通過させる必要があります。最初のパスでは、データの最も広い行を計算する必要がありますので、----行を作成することができます。次に、構築している出力タイプ(ここではSystem.out)にlikeを追加してから、データを再度歩いて出力に追加します。改行やその他のターミネータのようなものを追加する必要があります。列を整列させる場合は、同じことを行いますが、最初のステップでは、別の多次元配列に各列のデータが最も広く記録され、出力時に各列の幅が最も広いパッドデータが記録されます---行の幅を変更して、その行を構築する前にこれを計算する必要があります)。これらのアイデアのいくつかはで少し修正ここ

はあなたの例である(ただし、列を整列するためにパディングないあなたがするために、つまり、それは簡単に信頼私です)答えるため

public static void main(String[] args) throws Exception { 

    /* Adding values to the 2D Array. */ 

    String[][] data = { { "John", "99", "88", "89" }, 
      { "May", "99", "100", "100" } }; 

    int wideRowWidth = 0; 
    int curRowWidth; 

    // WALK DATA ONCE TO FIND THE WIDEST ROW 
    for (int i = 0; i < data.length; i++) { 
     curRowWidth = 0; 
     for (int j = 0; j < data[i].length; j++) { 
      curRowWidth += data[i][j].length(); 
     } 
     if (curRowWidth > wideRowWidth) { 
      wideRowWidth = curRowWidth; 
     } 

    } 

    // BUILD YOUR DASHED LINE 
    for (int i = 0; i < wideRowWidth + data[0].length -1; i++) { 
      System.out.print("-"); 
    } 
    System.out.print("\n"); 

    // USE YOUR DATA 
    for (int i = 0; i < data.length; i++) { 
     for (int j = 0; j < data[i].length; j++) { 
      System.out.print(data[i][j] + " "); 
     } 
     System.out.print("\n"); 
    } 
} 
関連する問題