By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,467 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,467 IT Pros & Developers. It's quick & easy.

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

P: n/a
I'm having trouble finding the error on my code. I know what "Exception thread ... ArrayIndexOutOfBounds... " means but I couldn't tell which part in the looping it is or in other.


Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.*;
  3. public class RoundRobin
  4. {
  5. static Scanner console = new Scanner (System.in);
  6. public static void main (String[]args)throws Exception
  7.     {
  8.     System.out.println("ROUND ROBIN SCHEDULING");
  9.     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");
  10.  
  11. int number;
  12. System.out.print("\n\nEnter no. of Jobs from 1 - 10: ");
  13. number = console.nextInt();
  14.  
  15. if (number < 1 || number > 10)
  16. {
  17. System.out.println("Enter no. of jobs from 1 to 10 only!");
  18. System.out.print("\nEnter number of jobs (1 to 10 only):");
  19.     number=console.nextInt();    
  20. }
  21.  
  22. if (number >= 1 || number <= 10)
  23. {
  24.     int ctr;
  25.     int pass;
  26.     int keep;
  27.     int[] cpub = new int[number];
  28.     int[] mirrorc = new int[number];
  29.     int[] tat = new int[number];
  30.  
  31.     for (ctr=0; ctr<number; ctr++)
  32.     {
  33.      System.out.print("\nEnter CPU Burst of Job "+ (ctr+1) +": ");
  34.          cpub[ctr] = console.nextInt();
  35.          mirrorc[ctr] = cpub[ctr];
  36.     }
  37.  
  38.     if (cpub[ctr]<1)
  39.         {
  40.         System.out.print("\nEnter CPU Bursts not equal to or lesser than 0!");
  41.         System.out.print("Enter CPU Burst of Job "+ (ctr+1) +": ");
  42.         cpub[ctr] = console.nextInt();
  43.         }
  44.  
  45.  
  46.         for (pass = 0; pass < number-1; pass++)
  47.     {
  48.     for (ctr = 0; ctr < number-1; ctr++)
  49.     {
  50.     if (cpub[ctr] > cpub[ctr + 1])
  51.     {
  52.     keep = cpub[ctr] = cpub[ctr+1];
  53.     }
  54.     }
  55.     }
  56.  
  57.     for (pass = 0; pass < number-1; pass++)
  58.     {
  59.     for (ctr = 0; ctr < number-1; ctr++)
  60.     {
  61.     if (mirrorc[ctr] > mirrorc[ctr + 1])
  62.     {
  63.     keep = mirrorc[ctr] = mirrorc[ctr+1];
  64.     }
  65.     }
  66.     }
  67.  
  68.  
  69.     System.out.print("\nEnter Quantum Time: ");
  70.     int qt = console.nextInt();
  71.  
  72.         if (qt < mirrorc[ctr] || qt > mirrorc[ctr-1])
  73.     {
  74.     System.out.println("Invalid QT! Enter QT higher than the \nlowest job and lower than the highest job.");
  75.     System.out.print("\nEnter Quantum Time: ");
  76.     qt = console.nextInt();
  77.     }
  78.  
  79.     System.out.println("Arrangement of Jobs:");
  80.  
  81.  
  82.     int x;
  83.     x = ((mirrorc[number-1]) / qt) + 1;
  84.  
  85.     int y;
  86.     for (y = x; y >= 1; y --)
  87.     {
  88.     for (ctr = 0; ctr < number; ctr++);
  89.     {
  90.     if(cpub[ctr] > qt)
  91.     {
  92.     cpub[ctr] = cpub[ctr] - qt;
  93.         System.out.println("\t" + (ctr + 1) + "  " + qt);                        
  94.     }
  95.  
  96.     else if (cpub[ctr] >= 1 && cpub[ctr] < qt)
  97.     {
  98.     System.out.println("\t"+(ctr + 1)+" " +cpub[ctr]);
  99.     cpub[ctr] = 0;
  100.     }
  101.  
  102.     else if (cpub[ctr] == qt)
  103.     {
  104.     System.out.println("\t"+(ctr + 1)+" " + cpub[ctr]);
  105.         cpub[ctr] = 0;
  106.     }
  107.     else if (cpub[ctr] == 0)
  108.     {
  109.     System.out.println("...");
  110.     }
  111.      }
  112.    }
  113. }
  114. }
  115. }
  116.  

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

Expand|Select|Wrap|Line Numbers
  1. ROUND ROBIN SCHEDULING
  2.  
  3.  
  4. NOTE: Quantum Time to be entered 
  5.     must be higher than the lowest Job's CPU Burst Value 
  6.     and must be lower than the highest Job's CPU Burst Value
  7.  
  8.  
  9. Enter no. of Jobs from 1 to 10 only: 3
  10.  
  11. Enter CPU Burst of Job 1: 4
  12.  
  13. Enter CPU Burst of Job 2: 6
  14.  
  15. Enter CPU Burst of Job 3: 2
  16. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
  17.     at RoundRobin.main(RoundRobin.java:38)
  18.  
  19. Process completed.
  20.  
Please help.
Oct 12 '10 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 303
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].
Oct 12 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.