Results 1 to 4 of 4
  1. #1
    TerrorBlade is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Unhappy Help...Cannot locate ArrayIndexOutOfBoundsException

    Hallo...

    i've got a project in my university and we just started using java...so i am new to all of this...anyway i wrote this code ( my problem is in the function merge ) and when i compile it it gives me this error

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    import java.util.Arrays;
    
    
    public class Lab2 {
    
       public static int[] Merge(int[][] A){
            
            //Define P
    		int P[]= new int[4];
    		for(int i=0;i<4;i++)
    		{
    			P[i]=0;
    		}
    
            int B[]= new int[A.length * A[0].length];
    		
           //Merge
    
            int w=0;
            while(w < A.length * A[0].length)
            {   
                int min=0; 
                int row=0;
                for(int i=0;i<A[i].length;i++)
                {
                    System.out.println("I:"+i);
                    for(int j=0;j<A.length;j++)
                    {
                        System.out.println("J:"+j);
                        if(A[j][P[i]]<min)
                        {
                            min=A[j][P[i]];
                            row=j;
                        }
                        B[w]=min;
                        w++;
                        if(row<A.length)
                        {
                            P[row]++;
                        }
                    }
                }
            }
            return B;
       }
        
    	 public static int[][] SortTable(int[][] D){
            
            //Sort Table
    
    		for(int i=0;i<D.length;i++)
            {
                for(int j=0;j<D[i].length;j++)
                {
                    for(int k=1;k<D[i].length-j;k++)
                    {
                        if(D[i][k]<D[i][k-1])
                        {
                            int e=D[i][k]; 
                            D[i][k]=D[i][k-1];
                            D[i][k-1]=e;
                          }
                    }
    
                }
            }
    		return D;
        }
    
        public static int[][] CreateTable(){
            
            //The user selects the N,M dimensions of array 
    		Scanner input=new Scanner(System.in);
            System.out.println("Give dimensions N,M");
    		int N=input.nextInt();
    		int M=input.nextInt();
            
            //Define Array 2D- 2nd
    		int D[][] = new int[N][M];
    
    		
    	
            //Create the 2D Array with random numbers
            Random randomGenerator = new Random();
    		for(int i=0;i<D.length;i++)
    		{
    			for(int j=0;j<D[i].length;j++)
    			{
    				D[i][j]=randomGenerator.nextInt(10);
    			}
    		}
    		return D;
        }
        
        //Print 2D Array
        public static void Print2D(int[][] D){
    		for (int i = 0; i < D.length; i ++)
    		{		
    			
    			for (int j = 0; j < D[i].length; j ++)
    			{	
    		
    				System.out.print("\tD[" + i + "] [" + j + "] = " + D[i][j]);
    			}
    			System.out.println();
    		}
        }
        
    	//Print 1D Array
        public static void Print1D(int[] D){
             for(int i=0;i< D.length;i++)
    		 {
                System.out.println("D[" + i + "] = " + D[i]);
    		 
            }
        }
    
        public static void main(String[] args) {
           
            System.out.println("Welcome to Lab 2!!!!!");
            
            int[][] D=CreateTable();
            
            System.out.println("UnSorted");
            Print2D(D);
            
    		System.out.println("Sort");
            int[][] SArray=SortTable(D);           
            Print2D(SArray);
     
            System.out.println("Merge");
            int[] C=Merge(SArray);
            
            Print1D(C);
        }
    }
    Welcome to Lab 2!!!!!
    Give dimensions N,M
    2
    3
    UnSorted
    D[0] [0] = 9 D[0] [1] = 3 D[0] [2] = 1
    D[1] [0] = 4 D[1] [1] = 0 D[1] [2] = 2
    Sort
    D[0] [0] = 1 D[0] [1] = 3 D[0] [2] = 9
    D[1] [0] = 0 D[1] [1] = 2 D[1] [2] = 4
    Merge
    I:0
    J:0
    J:1
    I:1
    J:0
    J:1
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at Lab2.Merge(Lab2.java:26)
    at Lab2.main(Lab2.java:138)


    if you could help me i would appreciate it :)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help...Cannot locate ArrayIndexOutOfBoundsException

    Your line #26 (see the error message) looks extremely suspicious:

    Java Code:
     for(int i=0;i<A[i].length;i++)
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    TerrorBlade is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default Re: Help...Cannot locate ArrayIndexOutOfBoundsException

    let me give you some info about the prog...we have one 2D array and we first sort and then we want to merge its lines to a 1D array ( sorted), but we want to make this possible by using a second array named P that it has cells as many as lines of the 2D array...and in the cells there is a pointer that shows us the column where the minimum of the line is located.

    lets say we have the array :

    2 5 7 9
    1 3 8 9

    the array B ( that is the 1D array) must have the numbers in that way : 1 2 3 5 7 8 9 9


    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    import java.util.Arrays;
    
    
    public class Lab2 {
    
       public static int[] Merge(int[][] A){
            
            //Define P
    		int P[]= new int[4];
    		for(int i=0;i<4;i++)
    		{
    			P[i]=0;
    		}
    
            int B[]= new int[A.length * A[0].length];
    		
           //Merge
    
            int w=0;
               
                int min=100;
                int row=0;
                for(int i=0;i<A.length;i++)
                {
                    System.out.println("I:"+i);
                    for(int j=0;j<A[i].length;j++)
                    {
                        System.out.println("J:"+j);
                        if(A[i][P[i]]<min)
                        {
                            min=A[i][P[i]];
                            row=j;
                        }
                        B[w]=min;
                        w++;
                        P[row]++;
                    }
                }
                for(int i=0;i<B.length;i++)
                {
                    System.out.println("B["+ i +"] = "+ B[i]);
                }
            return B;
       }
        
    	 public static int[][] SortTable(int[][] D){
            
            //Sort Table
    
    		for(int i=0;i<D.length;i++)
            {
                for(int j=0;j<D[i].length;j++)
                {
                    for(int k=1;k<D[i].length-j;k++)
                    {
                        if(D[i][k]<D[i][k-1])
                        {
                            int e=D[i][k]; 
                            D[i][k]=D[i][k-1];
                            D[i][k-1]=e;
                          }
                    }
    
                }
            }
    		return D;
        }
    
        public static int[][] CreateTable(){
            
            //The user selects the N,M dimensions of array 
    		Scanner input=new Scanner(System.in);
            System.out.println("Give dimensions N,M");
    		int N=input.nextInt();
    		int M=input.nextInt();
            
            //Define Array 2D- 2nd
    		int D[][] = new int[N][M];
    
    		
    	
            //Create the 2D Array with random numbers
            Random randomGenerator = new Random();
    		for(int i=0;i<D.length;i++)
    		{
    			for(int j=0;j<D[i].length;j++)
    			{
    				D[i][j]=randomGenerator.nextInt(10);
    			}
    		}
    		return D;
        }
        
        //Print 2D Array
        public static void Print2D(int[][] D){
    		for (int i = 0; i < D.length; i ++)
    		{		
    			
    			for (int j = 0; j < D[i].length; j ++)
    			{	
    		
    				System.out.print("\tD[" + i + "] [" + j + "] = " + D[i][j]);
    			}
    			System.out.println();
    		}
        }
        
    	//Print 1D Array
        public static void Print1D(int[] D){
             for(int i=0;i< D.length;i++)
    		 {
                System.out.println("D[" + i + "] = " + D[i]);
    		 
            }
        }
    
        public static void main(String[] args) {
           
            System.out.println("Welcome to Lab 2!!!!!");
            
            int[][] D=CreateTable();
            
            System.out.println("UnSorted");
            Print2D(D);
            
    		System.out.println("Sort");
            int[][] SArray=SortTable(D);           
            Print2D(SArray);
     
            System.out.println("Merge");
            int[] C=Merge(SArray);
            
            Print1D(C);
        }
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help...Cannot locate ArrayIndexOutOfBoundsException

    Do you have a question?

Similar Threads

  1. Replies: 4
    Last Post: 11-08-2011, 05:04 AM
  2. Cannot locate java installation
    By Javabeans in forum New To Java
    Replies: 1
    Last Post: 07-21-2011, 04:32 PM
  3. Problem with locate JRE
    By Bert600 in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-18-2010, 09:00 AM
  4. Could not locate named parameter
    By nc_newie in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 06-06-2009, 12:46 AM

Posting Permissions

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