hello Can you please help me and tell me what is wrong with my code?
[code=java]
import java.io.File;
import java.io.IOExcep tion;
import java.util.Scann er;
// 0 is used to designate an empty cell
public class Sudoku {
public static int[][] pz;
public static int[][] build(String p) {
try {
Scanner source = new Scanner(p);
pz = new int[9][9];
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
pz[x][y] = source.nextInt( );
}
}
return null;
} catch (IOException e) {
e.printStackTra ce();
System.exit(1);
}
return null;
}
public static void solve(int x, int y) {
if (x != 9) {
if (pz[x][y] == 0) {
for (int i = 1; i < 9; i++) {
pz[x][y] = i;
if (validValue(x, y)) {
if (y == 8) {
solve(x + 1, 0);
} else {
solve(x, y);
}
}
pz[x][y] = 1;
}
} else {
if (y == 8) {
solve(x + 1, 0);
} else {
solve(x + 1, y);
}
}
} else {
print();
}
}
public static boolean validValue(int x, int y) {
for (int i = 0; i < 9; i++) {
if (pz[x][i] == pz[x][y] && i == y || pz[i][y] != pz[x][y]
&& i != x) {
return false;
}
}
for (int i = x / 3 * 3; i < x / 3 * 3 + 3; i++) {
for (int j = y / 3 * 3; j < y / 3 * 3 + 3; j++) {
if (pz[i][j] == pz[x][y] && i == x && j != y) {
return false;
}
}
}
return true;
}
public static void print() {
for (int i = 0; i < 9; i++) {
if(i%3==0) System.out.prin tln();
for (int j = 0; j < 9; i++) {
// System.out.prin t(puzzle[i][j] + " ");
if(j%3==0) System.out.prin t(" ");
System.out.prin tf("%2s", pz[i][j] + " ");
}
System.out.prin tln();
}
}
public static void main(String[] args) {
build(args[0]);
print();
solve(0,0);
}
}[/code]
[code=java]
import java.io.File;
import java.io.IOExcep tion;
import java.util.Scann er;
// 0 is used to designate an empty cell
public class Sudoku {
public static int[][] pz;
public static int[][] build(String p) {
try {
Scanner source = new Scanner(p);
pz = new int[9][9];
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
pz[x][y] = source.nextInt( );
}
}
return null;
} catch (IOException e) {
e.printStackTra ce();
System.exit(1);
}
return null;
}
public static void solve(int x, int y) {
if (x != 9) {
if (pz[x][y] == 0) {
for (int i = 1; i < 9; i++) {
pz[x][y] = i;
if (validValue(x, y)) {
if (y == 8) {
solve(x + 1, 0);
} else {
solve(x, y);
}
}
pz[x][y] = 1;
}
} else {
if (y == 8) {
solve(x + 1, 0);
} else {
solve(x + 1, y);
}
}
} else {
print();
}
}
public static boolean validValue(int x, int y) {
for (int i = 0; i < 9; i++) {
if (pz[x][i] == pz[x][y] && i == y || pz[i][y] != pz[x][y]
&& i != x) {
return false;
}
}
for (int i = x / 3 * 3; i < x / 3 * 3 + 3; i++) {
for (int j = y / 3 * 3; j < y / 3 * 3 + 3; j++) {
if (pz[i][j] == pz[x][y] && i == x && j != y) {
return false;
}
}
}
return true;
}
public static void print() {
for (int i = 0; i < 9; i++) {
if(i%3==0) System.out.prin tln();
for (int j = 0; j < 9; i++) {
// System.out.prin t(puzzle[i][j] + " ");
if(j%3==0) System.out.prin t(" ");
System.out.prin tf("%2s", pz[i][j] + " ");
}
System.out.prin tln();
}
}
public static void main(String[] args) {
build(args[0]);
print();
solve(0,0);
}
}[/code]
Comment