简体   繁体   中英

Convert a 2d array to a organizing table look

I have convert a txt file into a 2d array, how can I make it look more organize?

My input file look like this:

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

Currently I get:

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

I want the data looks more like a table which is easier to read, how can I do that?

Code:

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

    }
}

Thank you very much.

try output using printf

or you can use Formatter

You can do it in this way, which is as follows :

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

Hope that might help.

Regards

I imagine your not going to want all that System.out stuff in your product but I am going to use it in an example, building on yours to show you the basic steps you will need to do.

Basically you need to make two passes over your data. First pass you should calculate the widest row of the data, so you can craft your ---- line. Then you add the like to whatever output type you are building (here it is System.out) and then walk the data again and add that to the output. You should add something like a newline or other terminator. If you want to line up the columns, you do the same thing but in the first step also record in another multi dimensional array the widest data in each column, and pad data that is shorter that the widest in each column when outputting (this will alter the width of your --- line so you will need to calculate this before building that line of course).

Here is your example modified a little bit with some of these ideas (not padding to line up the columns though, that is for you to do, it is easy trust me)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM