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 8691131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:03:48+00:00 2026-06-13T00:03:48+00:00

So, I asked a question 2 days ago, I’m going to try to rework

  • 0

So, I asked a question 2 days ago, I’m going to try to rework it to make it simpler:

I have a 2D array, which represents a Sudoku game.

I’m trying to check the game for errors as with a typical Sudoku game:

No number (1-9) is repeated within a row, column or 3×3 square. No empty cells.

The user IS ALLOWED to enter a repeat value, but the game will not be considered “won” until they fix these errors. I want to allow them to enter wrong values, just not win.

I’m very new to Java, so I had a limited knowledge to approach this with. I was going to try a long if, else statement comparing all the cells. That didn’t work, because -1 was repeated. (-1 represents an empty square). I tried to get around this, but realized that this if statement was too messy and there had to be a better way.

Then I thought to use boolean statements to test each number, setting to true if it had been seen before. That seems messy with so many boolean statements.

So, I’m kind of at a wall. Any tips on what to try? Try not to use anything advance, only been doing Java 2 months.

  • 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-13T00:03:49+00:00Added an answer on June 13, 2026 at 12:03 am

    1) You want to loop through the values, such as with a for loop. That will be much better than a long if-else-if chain.

    2) To keep track of values seen, a simple way for you would probably be to use a list. I’ll try to keep this example as simple as possible since you ask for “nothing advance.”

    public boolean checkRow(int rowNumber)
    {
        ArrayList numbersSeen = new ArrayList();
    
        for(int i = 0; i < 9; i += 1)
        {
            if(sudokuArray[i][rowNumber] != -1 && numbersSeen.contains(sudokuArray[i][rowNumber]))
            {
                return true;
            }
        }
    
        return false;
    }
    

    This will return true if row rowNumber has a number repeat in the 2D array specified by sudokuArray, and false otherwise. Notice the sudokuArray != -1, that takes into account the -1 placeholder for empty squares that you mentioned.

    When you compile something like this, if the compiler errors about trying to put an integer into the array list, you might have to make it generic by specifying ArrayList<Integer>. I do not recall if Java will auto-box a primitive for you when you specify a destination of type Object.

    This then leads into a small side-lesson which might be beneficial to you since you are new: Java has both primitive types (boolean, byte, short, int, long, float, double) and object versions of the primitive types (Boolean, Byte, Integer, etc.). If you do int i = 0; and Integer i2 = i;, Java will be nice enough to do the conversion for you.

    checkColumn would be very similar. check3X3area (or whatever you would want to call it) could be somewhat similar; perhaps you could use 2 for-loops, one nested inside the other, and loop 3 times each instead of 9. This will be left as an exercise for you.

    Also, you could make this generic to allow for sudoku boards that are not of size 9 with a few modifications.

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

Sidebar

Related Questions

Guys I've asked few days ago a question and didn't have really time to
About 3 days ago I asked a question, which can be found here: how
Few days ago I have asked a question about 1,2 and 3. degree connections.
I asked a question a couple days ago about creating INSERTs by running a
I asked a question a few days ago about how to customise the calendar
A few days ago I asked this question about jquery ajax function invoking action
Some days ago I asked a question about tagging differencies in 2 text files,
A few days ago I asked a question titled How to constraint a generic
A couple of days ago I asked a question about how to replace and
Two days ago I was asked this question in an interview for Data Analyst

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.