CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2004
    Posts
    49

    Question java.lang.ArrayIndexOutOfBoundsException: help

    hi

    i have writtten a program which incorporrates pascals triangle to calcuate n choose r, but i have followed the task instructions and seem to get an outofbounds error and cant seem to make out whats wrong:

    Code:
    public class TestPascal {
       
        public static void main(String[] args) {
            InputFrame theInputFrame = new InputFrame();
            while(true){
                int n = theInputFrame.getInt();
                int r = theInputFrame.getInt();
                //int maxN = (n > 10) ? n - 10: 0;
                //PascalTriangle test = new PascalTriangle(maxN);
                PascalTriangle test = new PascalTriangle(n-10);
                if (n > 10){
                System.out.println(n+", "+r);
                System.out.println(test.binomial(n,r));
                }   
            }
        }
    }
    this is the code for the test program which gets values for n and r

    this is the code that causes an error and i have highlighted the line:

    Code:
     
    
    public class PascalTriangle {
        private int maxN;
        private double[][] triangle;
        
        PascalTriangle(int maxN){
            
            this.maxN=maxN;
            
            triangle = new double[maxN+1][];
            
            for (int n = 0; n < triangle.length; n++) {
                triangle[n] = new double[n+1];
                
                for (int r = 0; r < triangle[n].length; r++) {
                    triangle[n][r] = -1;
                }
                triangle[n][n] = 1;
            }
            
        }
        
        
        public double binomial(int n, int r) {
            
           
            if (triangle[n][r] >= 0) {//line that causes the error in the outofbounds
                return triangle[n][r];
            } else {
                return triangle[n][r] = binomial(n - 1, r - 1) + binomial(n - 1, r);
       }
           
        }
    the binomial method is meant to do the follwoing:

    The method body is the same basic algorithm, except that if 0 <= n <= maxN and 0 <= r <= n then triangle[n][r] must be taken into account:

    --If triangle[n][r] is positive, then it is returned as the result.

    --Otherwise the result is calculated by the usual recursion but is then also assigned to triangle[n][r].

    this what i the output window does when i enter a value:

    30, 15

    java.lang.ArrayIndexOutOfBoundsException: 30
    at ex9.PascalTriangle.binomial(PascalTriangle.java:50)//this line is highlighted above
    at ex9.TestPascal.main(TestPascal.java:26)

    any help would be great

  2. #2
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776

    Re: java.lang.ArrayIndexOutOfBoundsException: help

    That recursive function, binomial, reduces n and r by 1. There is no check for when n or r are less than zero. Your problem is when n or r equal zero and this line is called.
    Code:
    return triangle[n][r] = binomial(n - 1, r - 1) + binomial(n - 1, r);
    This calls binomial(-1,whatever) and -1 is never an index into an array.
    "The Chicken and Rice MRE is not a personal lubricant."

  3. #3
    Join Date
    Oct 2004
    Posts
    49

    Re: java.lang.ArrayIndexOutOfBoundsException: help

    thanks Joe

    Code:
     
    
     public double binomial(int n, int r) {
            //System.out.println("n = " + n + "; r = " + r);
           
            if (r < 0 || r > n) {
                return 0;
            }
            
           if (triangle.length <= n) {
              return binomial(triangle.length - 1, r);
            }
            if (triangle[n].length <= r) {
               return binomial(n, triangle[n].length - 1);
            }
            if (triangle[n][r] >= 0) {
                return triangle[n][r];
            } else {
                return triangle[n][r] = binomial(n - 1, r - 1) + binomial(n - 1, r);
       }
           
        }
    this now does not throw an expetion but i am not sure if it is actally right i can calcualte numbers by using pascals triangle but the actual answear for n=30, r=15 is wrong

    any suggestions on how to change this method would be much appreciated

  4. #4
    Join Date
    Oct 2004
    Posts
    49

    Re: java.lang.ArrayIndexOutOfBoundsException: help

    i have tryed the above method fully and gives inaccurate values, can some one help me understand why this isnt working.

    thanks

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: java.lang.ArrayIndexOutOfBoundsException: help

    get a pencil and paper out and write down what the code is doing. start with n=30 and r=15.. and do yourself, what the computer does. at some point you'll see why it's wrong.

    i note there is a significant lack of comments in that code and hence it is likely you will make mistakes. if you had written the comments first, in plain english, of the algorithm to generate a pascal triangle, it stands a better chance of working correctly
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  6. #6
    Join Date
    Oct 2004
    Posts
    49

    Re: java.lang.ArrayIndexOutOfBoundsException: help

    hi thanks for the replies i have got a draft on paper but its long to work through 30 over 15 cause it gives a large number, but i have fixed the problem

    cjard your comments are rarely useful to me

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: java.lang.ArrayIndexOutOfBoundsException: help

    Quote Originally Posted by SamK21
    cjard your comments are rarely useful to me
    a Freudian slip, perhaps?

    Good or bad job reference?
    "You will be lucky to get this man to work for you..."
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured