user1093111
user1093111

Reputation: 1111

How to fill a 2d array column by column instead of row by row

I have a of a .txt financial statement that was downloaded from the internet. When it was downloaded the cells were taken column by column... screwing up my ability to rebuild the statement in a simple manner.

So my question is how to fill a 2d array column by column instead of row by row. Obviously in a loop you have to skip over the rows, but I am rusty and having trouble figuring this simple problem out. Help por favor.

this is the income statement I am trying to rebuild in 53 rows and 11 columns PASTEBUCKET.COM/2303

and my normal sorting way. Which most likely won't be of great use

    String [][] fs = new String[53][11];
    while (input.hasNextLine())
    {   
            for(int row=0;row<53;row++){
                for (int col=0;col<11;col++){
                fs[row][col] = input.nextLine();
                System.out.printf("%s\t",fs[row][col]);
                }
            }
    }

Upvotes: 0

Views: 5406

Answers (1)

Neil Forrester
Neil Forrester

Reputation: 5241

Simply reverse which for loop runs inside the other.

for (int col=0;col<11;col++){
    for (int row=0;row<53;row++){
        fs[row][col] = input.nextLine();
        System.out.printf("%s\t",fs[row][col]);
    }
}

Upvotes: 7

Related Questions