Results 1 to 5 of 5
  1. #1
    ladyvie is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

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

    I'm not used to using java but i'm using it now as I can't do this program about process scheduling in VB.net.. please help, i don't know which part of the code is my error, whether it's in the looping or on the other part. Here's the code:

    Java 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..


    Java 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!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Check what your program does: you enter number 3; an array with 3 elements is created and in a loop you fill the array using index variable ctr. When the loop has finished (ctr == 3 now) you try to test this:

    Java Code:
    if (cpub[ctr]<1) // <--- line 38
    and bang, you get an AIOOBE because your array only has three elements, number 0, 1 and number 2; you could've read that from the exception message: 3 isn't a valid index value at line 38 and ever so right it is.

    kind regards,

    Jos

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    instead of if (cpub[ctr]<1) just use cpub.length < 1 ..

    always safe to use length method to find the length of array
    Ramya:cool:

  4. #4
    ladyvie is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    THANK you both!

    I used .length and the quantum time is showing.. but still I can't fix it all, the "Exception in thread..." is still showing and now it's after the "Arrangement of Jobs" part.

    Sorry, my neurons are short in logic.. :D
    Please help..

  5. #5
    ladyvie is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Talking

    THANK you both!

    I used .length and the quantum time is showing.. but still I can't fix it all, the "Exception in thread..." is still showing and now it's after the "Arrangement of Jobs" part.

    Sorry, my neurons are short in logic.. :D
    Please help..

Similar Threads

  1. Replies: 8
    Last Post: 03-05-2011, 03:53 AM
  2. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  3. Replies: 5
    Last Post: 04-05-2010, 02:28 PM
  4. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Tags for this Thread

Posting Permissions

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