Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8893881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:21:34+00:00 2026-06-14T23:21:34+00:00

I am doing this Sudoku solver in java, and for some reason I have

  • 0

I am doing this Sudoku solver in java, and for some reason I have an error in my code that I just cant fix. My code has a guess function where it guesses numbers from 1-9 in each box while it checks if the number is already written before.

The error is in the line:

 else if (board[r + (i % 3)][c + (i / 3)] == num)

Where i get an ArithmeticException (divide by 0) for some reason I can’t see why. Hope you can help

My code:

public class SudokuSolver
{
final int size = 9;
private int box_size;

private int[][] board;

// Create an empty board
public SudokuSolver()
{
  board = new int[size][size];
  this.box_size = size / 3;

}

// initialize a given board
public SudokuSolver(int[][] board)
{
  this.board = board;
}

public void setCell(int num, int row, int col)
{
  board[row][col] = num;
}

public int getCell(int row, int col)
{
  return board[row][col];
}

private boolean check(int num, int row, int col)
{
  int r = (row / 3) * 3;
  int c = (col / 3) * 3;

  for (int i = 0; i < size; i++)
  {
     if (board[row][i] == num)
        return false;

     else if (board[i][col] == num)
        return false;

     else if (board[r + (i % box_size)][c + (i / box_size)] == num)
        return false;
  }
  return true;
  }

  public boolean guess(int row, int col)
  {
  int nextCol = (col + 1) % size;
  int nextRow = (nextCol == 0) ? row + 1 : row;

  try
  {
     if (board[row][col] != 0)
        return guess(nextRow, nextCol);
  }
  catch (ArrayIndexOutOfBoundsException e)
  {
     return true;
  }

  for (int i = 1; i <= size; i++)
  {
     if (check(i, row, col))
     {
        board[row][col] = i;
        if (guess(nextRow, nextCol))
        {
           return true;
        }
     }
  }
  board[row][col] = 0;
  return false;
  }

  public void printBoard()
  {
  for (int row = 0; row < size; row++)
  {
     for (int col = 0; col < size; col++)
     {
        System.out.print(board[row][col] + "  ");
     }
     System.out.println();
   }

  }



  public static void main(String[] args)
  {

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

  SudokuSolver ss = new SudokuSolver(board);
  ss.printBoard();
  System.out.println();
  System.out.println();
  if(ss.guess(0, 0))
     ss.printBoard();


 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:21:35+00:00Added an answer on June 14, 2026 at 11:21 pm

    Printing box_size before the point at which the error is encountered will reveal that it is in fact 0. You never initialize it in this constructor, so it retains its default value of 0:

    public SudokuSolver(int[][] board) {
        this.board = board;
    }
    

    You probably meant to include a line like this.box_size = board.length / 3.


    P.S. The real line that causes the error is

    else if (board[r + (i % box_size)][c + (i / box_size)] == num)
    

    You probably shouldn’t have substituted box_size for what you expected it to be in your question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing this interface where I have a lot of buttons that are just
I am doing this assignment, make a program that solves sudoku. I have a
I have some code doing this : var changes = document.getElementsByName(from); for (var c=0;
I am interested in doing this C code in Java: // sets n's ith
I'm doing a Sudoku solver and for that I want my JTextFields to only
As a programming exercise, I just finished writing a Sudoku solver that uses the
Doing this code kata and I have to remove as many of the if
I am doing a Sudoku puzzle solver for homework, but am encountering some difficulty.
Im doing something like this to get a list of all users that have
Is there anyway of doing this by using rules or by some custom code?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.