# Thread: Error in inverted Pascal triangle program

1. Member
Join Date
Nov 2012
Posts
2
Rep Power
0

## Error in inverted Pascal triangle program

Hi everyone. I donīt know why the following error appears when I try to execute my program, so if anyone can help me please... xD
Source code:
import java.util.*;

public class pascal2 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
int x = 0;

do {
System.out.print("How many values (1...13) ?");
x = in.nextInt();
} while ( x<1 || x>13 );

int y = (x*2)-1;
int [] a = new int [y];
int [][] b = new int [y][x];
int j = (x-1);
int k = y;

for ( x = 0 ; x <= j ; x++ ) {
for ( y = 0 ; y < k ; y++ ) {
if ( x == 0 ) {
if ( y == (((k+1)/2)-1) ) b[y][x] = 1;
else b[y][x] = 0;
}
else {
if ( y == 0 ) b[y][x] = b[y+1][x-1];
else {
if ( y == (k-1) ) b[y][x] = b[y-1][x-1];
else b[y][x] = b[y-1][x-1] + b[y+1][x-1];
}
}
}
}
for ( x = j ; x >= 0 ; x-- ) {
for ( y = 0 ; y <= k ; y++ ) {
if ( y == (k-1) ) {
if ( b[y][x] == 0 ) System.out.println(" ");
else System.out.println( b[y][x] );
}
else {
if ( b[y][x] == 0 ) System.out.print(" "); <-- line 43
else System.out.print( b[y][x] );
}
}
}
}
}

Error in cmd:
D:\>java pascal2
How many values (1...13) ?7
1 6 15 20 15 6 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at pascal2.main(pascal2.java:43)
Last edited by darkwolf1345; 11-15-2012 at 05:41 PM.

2. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
26

## Re: Error in inverted Pascal triangle program

Please use [code] tags [/code] when posting code.

I'm guessing here (as I don't read unformatted code), but you are accessing someArray[13] where someArray is only 13 elements long.
Arrays go from 0 to (arrayLength - 1).

3. Member
Join Date
Nov 2012
Posts
2
Rep Power
0

## Re: Error in inverted Pascal triangle program

I fixed it, thanks a lot (I knew arrays go from 0 to lenght-1, but you helped me remind it xD )

I didnt understand "
Java Code:
` tags`
", can you explain it some other way? because I dont want to botter anyone else with that, and I was hoping you could help me fix the spaces, because when the user selects a number of lines that contain numbers with more than one digit, the spaces go all wrong... and for you to help me you need to read the code xDDD

4. ## Re: Error in inverted Pascal triangle program

Guide For New Members