Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi experts. Good Evening.
I have sudoku solver in c. It is working properly. I just change into java coding. But it is not working. It return some error. I know little bit of java. Plz Help me.

C Coding :

C
#include <stdio.h>
  
int grid[9][9];
  
void print_solution(void)
{
  static int nsol = 0;
  int r, c;
  
  printf("----- solution %d -----\n", ++nsol);
  for (r = 0; r < 9; r++)
  {
    for (c = 0; c < 9; c++)
    {
      printf("%d", grid[r][c]);
      if (c % 3 == 2) printf("  ");
    }
    printf("\n");
    if (r % 3 == 2) printf("\n");
  }
  
}
  
int safe(int row, int col, int n)
{
  int r, c, br, bc;
  
  if (grid[row][col] == n) return 1;
  if (grid[row][col] != 0) return 0;
  for (c = 0; c < 9; c++)
    if (grid[row][c] == n) return 0;
  for (r = 0; r < 9; r++)
    if (grid[r][col] == n) return 0;
  br = row / 3;
  bc = col / 3;
  for (r = br * 3; r < (br + 1) * 3; r++)
    for (c = bc * 3; c < (bc + 1) * 3; c++)
      if (grid[r][c] == n) return 0;
  
  return 1;
}
  
void solve(int row, int col)
{
  int n, t;
  
  if (row == 9)
    print_solution();
  else
    for (n = 1; n <= 9; n++)
      if (safe(row, col, n)) {
    t = grid[row][col];
    grid[row][col] = n;
    if (col == 8)
      solve(row + 1, 0);
    else
      solve(row, col + 1);
  
    grid[row][col] = t;
      }
}
  
int
main()
{
  int i, j;
  
  printf("enter the sudoku: \n");
  for(i=0;i<9;i++)
    for(j=0;j<9;j++)
    {
      printf("(%d, %d): ", i+1, j+1);
      scanf("%d", &grid[i][j]);
    }
  solve(0,0);
  return 0;
}

I just do some changes in java coding.

Java coding:
Java
import java.util.*;
import java.io.*;

class puzzle
{
  
  public static void main(String[] args)
  {
   int[][] grid = new int[9][9];
   int i, j;
   Scanner in = new Scanner(System.in);
   System.out.println("Enter the Values :");
 
     for(i=0;i<9;i++)
      for(j=0;j<9;j++)
       {
        System.out.print(i+1+","+ j+1);
        
        grid[i][j]=in.nextInt();

       }
   solve(0,0);
   return 0;

  }

void print_solution()
{
  
  static int nsol = 0;
  int r, c;
  System.out.println("----- solution"+ ++nsol + " -----\n");
  for (r = 0; r < 9; r++)
  {
    for (c = 0; c < 9; c++)
    {
     System.out.print(grid[r][c]);
      
      if (c % 3 == 2) 
      System.out.print("  ");
    }
    System.out.println();
    if (r % 3 == 2) 
    System.out.println();
  }
  
}
  
int safe(int row, int col, int n)
{
  int r, c, br, bc;
  
  if (grid[row][col] == n) 
  return 1;
  
  if (grid[row][col] != 0) 
  return 0;
  
  for (c = 0; c < 9; c++)
    if (grid[row][c] == n) 
    return 0;
  
  for (r = 0; r < 9; r++)
    if (grid[r][col] == n) 
    return 0;
  
  br = row / 3;
  bc = col / 3;
  for (r = br * 3; r < (br + 1) * 3; r++)
    for (c = bc * 3; c < (bc + 1) * 3; c++)
      if (grid[r][c] == n) return 0;
  
  return 1;
}
  
void solve(int row, int col)
{
  int n, t;
  
  if (row == 9)
    print_solution();
  else
    for (n = 1; n <= 9; n++)
      if (safe(row, col, n)) {
    t = grid[row][col];
    grid[row][col] = n;
    if (col == 8)
      solve(row + 1, 0);
    else
      solve(row, col + 1);
  
    grid[row][col] = t;
      }
}
}


Please help me what error is happening?
Posted
Updated 18-Nov-13 13:06pm
v2
Comments
ZurdoDev 18-Nov-13 13:49pm    
What is the error?
Vijaydhas 18-Nov-13 22:41pm    
sudoku.java:32: illegal start of expression
static int nsol = 0;
^
1 error
Sergey Alexandrovich Kryukov 18-Nov-13 13:55pm    
"Not working..." is not informative at all. Without your information, resolving your problem is not easier then writing the solution from scratch, so why puzzling people more then necessary?
Besides, always use the debugger to locate the problem.
—SA
Vijaydhas 18-Nov-13 22:42pm    
I got this error message:
sudoku.java:32: illegal start of expression
static int nsol = 0;
^
1 error

Sorry for the incomplete information. I am new to java.
Sergey Alexandrovich Kryukov 18-Nov-13 23:18pm    
Thank you for clarification. I answered, please see.
—SA

1 solution

Static local variables are not allowed in Java. Even if they were — what would make you think that nsol needs to be static? Just remove this keyword. And lean some Java, using one of regular manuals. Don't mix it up with, for example, C++.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900