Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to turn a 9X9 grid into 9 3 X3 grids

807601Jun 4 2008 — edited Jun 4 2008
first off, just so i fail to write out waht i mean in english prooerly, im making a Sudoku puzzle, which consists of 9 3X3 grids and the general layout is 1 9X9 grid , and after 5 hrs of trying, i still havent managed to figure outa way to transform a 2D array of numbers going from
0,0 ... 0,8
1,0 .... 1,8
.....
....
8,0 ...... 8,8 
into something that looks like:
0,0 | 0,1 | 0,2 |1,0 | 1,1| 1,2| 2,0|2,1|2,2
0,3| 0,4| 0,5| 1,3|  1,4| 1,5 | 2,3 | 2,4| 2,5
0,6|0,7|0,8|1,6|1,7|1,8 | 2,6| 2,7|2,8
and so on like this till 8,8 
The code i used for this is :
	public static String[][] row_Box (String[][] row, String [][] box){
		for (int i =0; i <9; i ++){
			for(int j = 0; j <9 ; j++){
			box[i][j]="";
			}
		}
		for (int k = 2; k < 9 ; k +=3 ){
			for (int i = 0; i < 3 ; i++){
				for (int j = 0; j < 3 ; j++){
					box[k-2][j] = "" + row[i + k-2][j];
				}
				for (int j = 3; j < 6 ; j++){
					box[k-1][j] = "" + row[i + k-2][j];
				}
				for (int j = 6; j < 9 ; j++){
					box[k][j] = "" + row[i + k-2][j];
				}
			}
		}
	return box;
	}
any help is appreciated :


i also tried turning it into a 1D array and going from there .... but i seem to have a problem with turning it into a 2D array again ........ here is the code .... for some reason it says that the separator isnt in the oneDArray i used, but when i print that put, the separator is there ......
	public static String[] toOneD (String[][] twoDArray, String separator){
		String[] oneDArray = new String [9];
		for (int i = 0; i <9; i ++){
			oneDArray[i] = "";
			for(int j = 0; j <9; j ++){
				oneDArray[i] += twoDArray[i][j] + separator; 
			}
		}
		return oneDArray;
	}
	
	public static String[][] toTwoD (String [] oneDArray, String separator){
		int index;
		String[][] twoDArray = new String [9][9];
		String[] tempOneDArray = new String [9];
		for(int i = 0; i <9; i ++){
			for(int j = 0; j <9; j ++){
				tempOneDArray[i] = oneDArray;
System.out.println (oneDArray[i]);// + tempOneDArray[i]);
}
}
for(int i = 0; i <9; i ++){
for(int j = 0; j <9; j ++){
//tempOneDArray[i] = oneDArray[i];
//System.out.println (oneDArray[i] + tempOneDArray[i]);
}
}
for(int i = 0; i <9; i ++){
for(int j = 0; j <9; j ++){
index = oneDArray[i].indexOf(separator);
System.out.print(index);
if (index > 0 && index < tempOneDArray[i].length()){
twoDArray[i][j] = tempOneDArray[i].substring(0, index+1);
tempOneDArray[i] = tempOneDArray[i].substring(index+1,tempOneDArray[i].length());
}
else {
//tempOneDArray[i] = oneDArray[i].substring(1,oneDArray[i].length());
twoDArray [i][j] = "LLLL";
}
}
}
return twoDArray;
}
Edited by: brishu on Jun 4, 2008 5:22 AM

Edited by: brishu on Jun 4, 2008 5:24 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 2 2008
Added on Jun 4 2008
4 comments
1,563 views