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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Buena Velasco
    New Member
    • Oct 2010
    • 1

    #1

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

    I know what the problem means, it's just that I can't find which part it is, is it in the looping or on the other part. The error says it's in line 38 but I can't really figure it out.

    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 to 10 only: ");
    		number = console.nextInt();
    		
    		if (number < 1 || number > 10)
    		{
    			System.out.println("Enter number 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, it display the error just after asking the jobs' CPU Bursts.. the time quantum isn't even shown..
    Code:
    --------------------Configuration: <Default>--------------------
    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!
  • Ian Smith

    #2
    You are using the for/loop variable *ctr* outside of the loop.

    Comment

    Working...