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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:56:04+00:00 2026-06-02T00:56:04+00:00

My indexoutofbounds issues has been solved but not my program compiles but prints out

  • 0

My indexoutofbounds issues has been solved but not my program compiles but prints out an unchanged puzzle.. not sure where I am going wrong?? The original puzzle read roms std input has 0s in the place of an ’empty’cell of a sudoku puzzle. I’ve included my arraylist initialiser as well.

public ArrayList<Integer> create(){

    ArrayList<Integer> possible = new ArrayList<Integer>(); 

    for(int i=1; i<10; i++){
        possible.add(i);
    }
    return possible;
  }
  public sudoku( int size )
  {
    SIZE = size;
    N = size*size;

    Grid = new int[N][N];
    for( int i = 0; i < N; i++ ) 
        for( int j = 0; j < N; j++ ) 
            Grid[i][j] = 0;
   }

  public void solve()
  { 
    int a, b, c, d, i, j, k, l; 

    int count = 0;
    int value= 0;

    for(i=0; i<N;i++){
        for(j=0; j<N;j++){  
            if(Grid[i][j]==0){

                ArrayList<Integer> possible = create();

                //check row             
                for(a=0; a<N;a++){
                    for(b=0; b<N; b++){  
                        if(Grid[a][0]==possible.get(a)){
                            possible.set(a, 0);
                        }
                    }
                }
                //check column
                for(c=0; c<N;c++){
                    for(d=0; d<N;d++){  
                        if(Grid[0][d]==possible.get(d)){
                            possible.set(d,0);
                        }
                    }
                }
                for(k=0; k<9; k++){
                    if(possible.get(k)!=0){
                        count++;
                    }
                }
                if(count==1){
                    for(l=0; l<9; l++){
                        if(possible.get(l)!=0){
                            value=possible.get(l);
                        }
                    }
                }
                Grid[i][j]=value;
            }
        }
    }
}
  • 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-02T00:56:06+00:00Added an answer on June 2, 2026 at 12:56 am

    I see your problem, you’re using the i and j variables as indices more than once in nested for loops:

      for (i = 0; i < N; i++) { // **** you use "i" it here
         for (j = 0; j < N; j++) { // **** and "j" here
            if (Grid[i][j] == 0) {
    
               ArrayList<Integer> possible = create();
    
               for (i = 0; i < N; i++) { // **** and again here
                  for (j = 0; j < N; j++) { // **** and again here
                     if (Grid[i][0] == possible.get(i)) {
                        possible.set(i, 0);
                     }
                  }
               }
    
               for (i = 0; i < N; i++) { // **** and again here
                  for (j = 0; j < N; j++) { // **** and again here
                     if (Grid[0][j] == possible.get(i)) {
                        possible.set(i, 0);
                     }
                  }
               }
    
               // ....
    
               Grid[i][j] = value;
            }
         }
      }
    

    By advancing the indices from within the for loop, you risk going past the maximal index, and so by the time you reach the bottom, your i and j have been incremented all the way to 9, past the size of the row and column. You should almost never change a for loop index from inside the for loop. You will want to re-write this code.

    Edit: it’s even simpler than that: You’re checking i after a for loop has concluded, so that i is the value of the top bound. Run this to see what I mean:

      for (i = 0; i < N; i++) {
         for (i = 0; i < N; i++) {
            System.out.println("C) i = " + i);
         }
         System.out.println("D) i = " + i);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's my first question, but I'll try to ask it well. Anyway, I'm trying
I'm writing a program in java that essentially tests a bunch of things... For
I have an input form that has 38 fields I know it's too much
Either I'm doing this wrong or i'm not understanding how this method works. ArrayList<String>
I apologize for the limited title, but I don't know how to describe this
I'm trying to create a list of objects and then a sublist and then
I have a collection of objects that's constantly changing, and I want to display
Special thanks to Rex M for this bit of wisdom : public IEnumerable<Friend> FindFriends()
I am having difficulties w/ writing and reading an array of objects from a
I'm writing android app, which drawing 4 graphs in 1 plot. Graph data is

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.