Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Buena Velasco

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:

    I'm having trouble finding the error on my code. I know what "Exception thread ... ArrayIndexOutOf Bounds... " means but I couldn't tell which part in the looping it is or in other.


    Code:
     
    import java.util.*;
    public class RoundRobin
    {
    static Scanner console = new Scanner (System.in);
    public static void main (String[]args)throws Exception
    	{
    	System.out.println("ROUND ROBIN SCHEDULING");
    	System.out.println("\n\nNOTE: Quantum Time to be entered \n\tmust be higher than the lowest Job's CPU Burst Value \n\tand must be lower than the highest Job's CPU Burst Value");
    		
    int number;
    System.out.print("\n\nEnter no. of Jobs from 1 - 10: ");
    number = console.nextInt();
    		
    if (number < 1 || number > 10)
    {
    System.out.println("Enter no. of jobs from 1 to 10 only!");
    System.out.print("\nEnter number of jobs (1 to 10 only):");
    	number=console.nextInt();	
    }
    		
    if (number >= 1 || number <= 10)
    {
    	int ctr;
    	int pass;
    	int keep;
    	int[] cpub = new int[number];
    	int[] mirrorc = new int[number];
    	int[] tat = new int[number];
    				
    	for (ctr=0; ctr<number; ctr++)
    	{
    	 System.out.print("\nEnter CPU Burst of Job "+ (ctr+1) +": ");
             cpub[ctr] = console.nextInt();
             mirrorc[ctr] = cpub[ctr];
    	}
    					
    	if (cpub[ctr]<1)
            {
            System.out.print("\nEnter CPU Bursts not equal to or lesser than 0!");
            System.out.print("Enter CPU Burst of Job "+ (ctr+1) +": ");
            cpub[ctr] = console.nextInt();
            }
    					
    					
            for (pass = 0; pass < number-1; pass++)
    	{
    	for (ctr = 0; ctr < number-1; ctr++)
    	{
    	if (cpub[ctr] > cpub[ctr + 1])
    	{
    	keep = cpub[ctr] = cpub[ctr+1];
    	}
    	}
    	}
    					
    	for (pass = 0; pass < number-1; pass++)
    	{
    	for (ctr = 0; ctr < number-1; ctr++)
    	{
    	if (mirrorc[ctr] > mirrorc[ctr + 1])
    	{
    	keep = mirrorc[ctr] = mirrorc[ctr+1];
    	}
    	}
    	}
    				
    				
    	System.out.print("\nEnter Quantum Time: ");
    	int qt = console.nextInt();
    	
            if (qt < mirrorc[ctr] || qt > mirrorc[ctr-1])
    	{
    	System.out.println("Invalid QT! Enter QT higher than the \nlowest job and lower than the highest job.");
    	System.out.print("\nEnter Quantum Time: ");
    	qt = console.nextInt();
    	}
    				
    	System.out.println("Arrangement of Jobs:");
    						
    					
    	int x;
    	x = ((mirrorc[number-1]) / qt) + 1;
    						
    	int y;
    	for (y = x; y >= 1; y --)
    	{
    	for (ctr = 0; ctr < number; ctr++);
    	{
    	if(cpub[ctr] > qt)
    	{
    	cpub[ctr] = cpub[ctr] - qt;
            System.out.println("\t" + (ctr + 1) + "  " + qt);						
    	}
    								
    	else if (cpub[ctr] >= 1 && cpub[ctr] < qt)
    	{
    	System.out.println("\t"+(ctr + 1)+" " +cpub[ctr]);
    	cpub[ctr] = 0;
    	}
    
    	else if (cpub[ctr] == qt)
    	{
    	System.out.println("\t"+(ctr + 1)+" " + cpub[ctr]);
            cpub[ctr] = 0;
    	}
    	else if (cpub[ctr] == 0)
    	{
    	System.out.println("...");
    	}
         }
       }
    }
    }
    }

    I'm getting this output, which displays the error just after entering CPU Bursts of jobs... the quantum time doesn't even show up:

    Code:
    ROUND ROBIN SCHEDULING
    
    
    NOTE: Quantum Time to be entered 
        must be higher than the lowest Job's CPU Burst Value 
        and must be lower than the highest Job's CPU Burst Value
    
    
    Enter no. of Jobs from 1 to 10 only: 3
    
    Enter CPU Burst of Job 1: 4
    
    Enter CPU Burst of Job 2: 6
    
    Enter CPU Burst of Job 3: 2
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
        at RoundRobin.main(RoundRobin.java:38)
    
    Process completed.
    Please help.
  • improvcornartist
    Recognized Expert Contributor
    • May 2007
    • 303

    #2
    Line 38, if (cpub[ctr]<1), is after the for loop in which ctr is incremented. So after the for loop, ctr is bigger than the number of elements in cpub. Example, you initialize cpub[0], cpub[1], cpub[2], and after the initializations it continues with ctr and tries to access cpub[3].

    Comment

    Working...