change statements to get a vertical format of output

Hi All,

I am trying to understand how to make a change  this BufferedWrite statements to get the output to be a vertical stack.  As shown below, I have edited it in order to demonstrate the present output. There are 2 unseen code marks as between  "},  {"  which I have  removed to show the output.


Horizontal:
{{6,8,5,2,9,7,4,3,1 },  {4,2,9,1,3,6,7,8,5 },  {3,1,7,8,5,4,2,9,6 },  {9,4,8,5,2,1,6,7,3 },  {7,6,3,9,4,8,5,1,2 },  {2,5,1,7,6,3,9,4,8 },  {8,7,6,4,1,2,3,5,9 },  {1,9,2,3,7,5,8,6,4 },  {5,3,4,6,8,9,1,2,7 }}


Vertical:

{{6,8,5,2,9,7,4,3,1 },
{4,2,9,1,3,6,7,8,5 },
{3,1,7,8,5,4,2,9,6 },
{9,4,8,5,2,1,6,7,3 },
{7,6,3,9,4,8,5,1,2 },
{2,5,1,7,6,3,9,4,8 },
{8,7,6,4,1,2,3,5,9 },
{1,9,2,3,7,5,8,6,4 },
{5,3,4,6,8,9,1,2,7 }}


try {
            FileOutputStream  fos= new
FileOutputStream("MySSolution.txt");
            BufferedWriter bow = new BufferedWriter(new
OutputStreamWriter(fos));
            bow.write("{");
            for(int xc = 0; xc< 9; xc++){
                bow.write("{");
                for(int yc = 0; yc < 9; yc++){
                    if(yc == 8){
                        bow.write( board[xc][yc].getCellValue( )+" ");
                    }
                    else  bow.write( board[xc][yc].getCellValue(
)+",");
                }
                if(xc == 8){
                    bow.write( "}}"+"\n"+"\n");
                }
                else bow.write( "},"+"\n"+"\n");
            }
            bow.close();
        }
        catch (IOException ioe) {
            System.out.println("IOException writing
MySudokuSolution.txt");
}

Delphi3
LVL 4
delphi3Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Always avoid using index literals in your code as it makes the code harder to maintain. This will do what you want for int[][] named 'a', which should be easily adaptable to your 'board':

            if (a.length > 0) {
                  System.out.print("{");
            }
            for (int h = 0; h < a.length; h++) {
                  for (int i = 0; i < a[h].length; i++) {
                        if (i == 0) {
                              System.out.print("{");
                        }
                        System.out.print(a[h][i]);
                        if (i < a[h].length - 1) {
                              System.out.print(",");
                        }
                  }
                  if (h < a.length - 1) {
                        System.out.println("},");
                  } else {
                        System.out.println("}}");
                  }
            }


//===================================================================

Output, given the array you stated:

{{6,8,5,2,9,7,4,3,1},
{4,2,9,1,3,6,7,8,5},
{3,1,7,8,5,4,2,9,6},
{9,4,8,5,2,1,6,7,3},
{7,6,3,9,4,8,5,1,2},
{2,5,1,7,6,3,9,4,8},
{8,7,6,4,1,2,3,5,9},
{1,9,2,3,7,5,8,6,4},
{5,3,4,6,8,9,1,2,7}}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
delphi3Author Commented:
Hi CEHJ,

Thanks for stopping to look at this.

Here is the adaptation:

        try {
            FileOutputStream  fos= new FileOutputStream("MySSolution.txt");
            BufferedWriter bow = new BufferedWriter(new OutputStreamWriter(fos));
            if (board.length > 0) {
                bow.write("{");
            }
            for (int h = 0; h < board.length; h++) {
                for (int i = 0; i < board[h].length; i++) {
                    if (i == 0) {
                        bow.write("{");
                    }
                    bow.write(board[h][i].getCellValue( )+ "" ); // the extra "" is needed to get an

number output
                    if (i < board[h].length - 1) {
                        bow.write(",");
                    }
                }
                if (h < board.length - 1) {
                    bow.write("},");
                    bow.newLine(); // added this
                } else {
                    bow.write("}}");
                }
            }
            bow.close();
        }
        catch (IOException ioe) {
            System.out.println("IOException writing MySSolution.txt");
        }
   

CEHJ,
 These "//" comments shown in my revision  are additional items needed to get this to work with the bow.write :

1.   >> + "" <<  needed for the board[h][i].getCellValue( ) data items to show at all.

2.  >> bow.newLine();  <<< if not  added here, the output is again horizontal.


These are the strangest bunch of adjustments necessary to get the desired results.
Thank goodness  for my having a second  IDE that shows all the possible code completion items for "bow.".

Yet this second IDE with capability of showing "code  completion" items for bow., during a run can't find the "main" in this original program from which all this was a part of.  It usually can, however.
 
I  use 2 IDE lightweights on my desktop, just for this possibility.
 
Thanks again.

D3
CEHJCommented:
An easier way to adapt the example would be to use a PrintWriter. You could then, after PrintWriter out = xxxxxx, simply remove 'System.' in my example and the printing code statements at least would be the same
CEHJCommented:
:-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.