Sudoku Programmers Forum Index

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log inLog in          Games  Calendar

Log in to check your private messagesLog in to check your private messages   

Converting int grid to string (Java)

 
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Programming sudoku
View previous topic :: View next topic  
Author Message
badgerking

Joined: 02 Apr 2009
Posts: 8
:

Items
PostPosted: Thu Apr 02, 2009 12:43 pm    Post subject: Converting int grid to string (Java) Reply with quote

Hi there, I am working on a sudoku program at the moment in Java.
At the moment I have been able to print a grid to the the output window (System.out...), solve the grid by brute force and then clear the grid (all in the output window).

I am now trying to transfer the grid into a GUI. The grid is represented as such


Code:

   static int[][] grida =             
    {
        {0, 1, 5, 4, 0, 0, 0, 0, 0},
        {0, 7, 0, 0, 3, 8, 0, 5, 2},
        {0, 0, 8, 0, 5, 0, 1, 0, 7},
        {0, 9, 0, 0, 0, 0, 0, 0, 4},
        {0, 8, 2, 0, 0, 0, 3, 1, 0},
        {6, 0, 0, 0, 0, 0, 0, 2, 0},
        {1, 0, 4, 0, 9, 0, 6, 0, 0},
        {2, 3, 0, 8, 6, 0, 0, 7, 0},
        {0, 0, 0, 0, 0, 3, 2, 9, 0},
    };



I have been able to display a grid in GUI but it has read it from a string. Is there a way I can convert this int[][] grida into a string.

Any help and thoughts I would be extremely grateful.
Back to top
View user's profile Send private message
Pete

Joined: 09 Jun 2008
Posts: 18
:
Location: Somerset, NJ

Items
PostPosted: Thu Apr 02, 2009 3:44 pm    Post subject: Reply with quote

If you just want a quick and easy conversion, you might try
Code:
Arrays.deepToString(grida)

If you're looking for any kind of fancy formatting of the string, you'll probably have to do it yourself.

Pete


Last edited by Pete on Fri Apr 03, 2009 2:22 am; edited 1 time in total
Back to top
View user's profile Send private message AIM Address
badgerking

Joined: 02 Apr 2009
Posts: 8
:

Items
PostPosted: Thu Apr 02, 2009 4:16 pm    Post subject: Reply with quote

Cheers for the advice Pete tried it out by printing the string out in the output but just came up as gobble-di-goop.

I have this method for reading a string

Code:

   
    public void initializeFromString(final String boardStr) {
       
        //clear();  // Clear all values from the board.
       
        int emptycells = 0; // parameter used to count the umber of empty cells
        row = 0;
        col = 0;
       
        for (int i = 0; i <boardStr>= '1' && c <='9') {
                if (row > 9 || col > 9) {
                    throw new IllegalArgumentException("SudokuModel: "
                            + " Attempt to initialize outside 1-9 "
                            + " at row " + (row+1) + " and col " + (col+1));
                }
                grid[row][col] = c - '0';  // Translate digit to int.
                col++;
            }
            else if (c == '.')
            {
                getVal(row,col);
                col++;
       
            }
            else if (c == '/') {
                row++;
                col = 0;
            }
            else if (c == ','){
                col++;
                emptycells++;
            }
            else
            {
                throw new IllegalArgumentException("SudokuModel: Character '" + c
                        + "' not allowed in board specification");
            }
           
        }
        System.out.printf("number of cells left = " + emptycells ); // prints out how mnay cells are empty
    }


is there a way i could modify this to read grida once converted into a string?Any help greatly appreciated.
Back to top
View user's profile Send private message
badgerking

Joined: 02 Apr 2009
Posts: 8
:

Items
PostPosted: Thu Apr 02, 2009 4:33 pm    Post subject: Reply with quote

just to clear up the initaliseString reads this

[code]
private static final String INITIAL_BOARD=

"..,....../" + "......,../" + "..,..,.../" + "...,.,.../" +
".....,,../" + ".,...,.../" + "...,..,../" + "..,.,.,../" +
"..,.,,.../";
[code]

this appears currently in GUI class

i want the initaliseString to read grida (see above) but that is an array of an array of int. therefore it needs to be converted[/code]
Back to top
View user's profile Send private message
Pete

Joined: 09 Jun 2008
Posts: 18
:
Location: Somerset, NJ

Items
PostPosted: Sun Apr 05, 2009 1:51 pm    Post subject: Reply with quote

The string produced by
Code:
Arrays.deepToString(grida)

is hardly what I would call "gobble-di-goop." Let's take a closer look.

Here's the string:
Code:
[[0, 1, 5, 4, 0, 0, 0, 0, 0], [0, 7, 0, 0, 3, 8, 0, 5, 2], [0, 0, 8, 0, 5, 0, 1, 0, 7], [0, 9, 0, 0, 0, 0, 0, 0, 4], [0, 8, 2, 0, 0, 0, 3, 1, 0], [6, 0, 0, 0, 0, 0, 0, 2, 0], [1, 0, 4, 0, 9, 0, 6, 0, 0], [2, 3, 0, 8, 6, 0, 0, 7, 0], [0, 0, 0, 0, 0, 3, 2, 9, 0]]


It may not look like much at first. However, let's replace the commas with newlines. With a tiny bit of formatting, we can see your original integer array in a more useful format. (There are several ways to do this.) Here's what we get:

Code:
[[0, 1, 5, 4, 0, 0, 0, 0, 0]
 [0, 7, 0, 0, 3, 8, 0, 5, 2]
 [0, 0, 8, 0, 5, 0, 1, 0, 7]
 [0, 9, 0, 0, 0, 0, 0, 0, 4]
 [0, 8, 2, 0, 0, 0, 3, 1, 0]
 [6, 0, 0, 0, 0, 0, 0, 2, 0]
 [1, 0, 4, 0, 9, 0, 6, 0, 0],
 [2, 3, 0, 8, 6, 0, 0, 7, 0]
 [0, 0, 0, 0, 0, 3, 2, 9, 0]]


Is this what you're trying to do?

Pete
Back to top
View user's profile Send private message AIM Address
badgerking

Joined: 02 Apr 2009
Posts: 8
:

Items
PostPosted: Sun Apr 05, 2009 3:04 pm    Post subject: Reply with quote

Thanks for the help pete.

I managed to get to convert the grid into a string but now, like you said, i need to format the string so it can be read and then displayed in the GUI.

MMy GUI can display a string but the string is defined in the GUI class. So it doesn't display the actual grid.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Programming sudoku All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Sudoku Programmers topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group

Igloo Theme Version 1.0 :: Created By: Andrew Charron