将2d数组转换为组织表外观

时间:2012-01-12 17:18:20

标签: java multidimensional-array

我已将txt文件转换为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("");

    }
}

非常感谢。

3 个答案:

答案 0 :(得分:1)

使用printf

尝试输出

或者您可以使用Formatter

答案 1 :(得分:0)

你可以这样做,如下:

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

希望这可能会有所帮助。

此致

答案 2 :(得分:0)

我想你不会想要你的产品中的所有System.out东西,但我将在一个例子中使用它,在你的基础上向你展示你需要做的基本步骤。

基本上你需要对你的数据进行两次传递。首先你应该计算最宽的数据行,这样你就可以制作你的----行。然后将like添加到您正在构建的任何输出类型(此处为System.out),然后再次遍历数据并将其添加到输出中。您应该添加类似换行符或其他终结符的内容。如果你想排列列,你做同样的事情,但在第一步也记录在另一个多维数组中每列中最宽的数据,并填充数据,输出时每个列中最宽的填充数据(这将改变你的---线的宽度,所以你需要在构建那条线之前计算它。

这里有一些你的例子修改了一些这些想法(虽然没有填充排列列,但是你可以这样做,很容易相信我)

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