Link to home
Start Free TrialLog in
Avatar of delphi3
delphi3

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of delphi3
delphi3

ASKER

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
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
:-)