473,431 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,431 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class RoundRobin
  3. {
  4.     static Scanner console = new Scanner (System.in);
  5.     public static void main (String[]args)throws Exception
  6.     {
  7.         System.out.println("ROUND ROBIN SCHEDULING");
  8.         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");
  9.  
  10.         int number;
  11.  
  12.         System.out.print("\n\nEnter no. of Jobs from 1 to 10 only: ");
  13.         number = console.nextInt();
  14.  
  15.         if (number < 1 || number > 10)
  16.         {
  17.             System.out.println("Enter number 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.  
  58.                     for (pass = 0; pass < number-1; pass++)
  59.                     {
  60.                         for (ctr = 0; ctr < number-1; ctr++)
  61.                         {
  62.                             if (mirrorc[ctr] > mirrorc[ctr + 1])
  63.                             {
  64.                                 keep = mirrorc[ctr] = mirrorc[ctr+1];
  65.                             }
  66.                         }
  67.                     }
  68.  
  69.  
  70.                 System.out.print("\nEnter Quantum Time: ");
  71.                 int qt = console.nextInt();
  72.  
  73.                 if (qt < mirrorc[ctr] || qt > mirrorc[ctr-1])
  74.                 {
  75.                     System.out.println("Invalid QT! Enter QT higher than the \nlowest job and lower than the highest job.");
  76.                     System.out.print("\nEnter Quantum Time: ");
  77.                     qt = console.nextInt();
  78.                 }
  79.  
  80.                     System.out.println("Arrangement of Jobs:");
  81.  
  82.  
  83.                         int x;
  84.                         x = ((mirrorc[number-1]) / qt) + 1;
  85.  
  86.                         int y;
  87.                         for (y = x; y >= 1; y --)
  88.                         {
  89.                             for (ctr = 0; ctr < number; ctr++);
  90.                             {
  91.                                 if(cpub[ctr] > qt)
  92.                                 {
  93.                                     cpub[ctr] = cpub[ctr] - qt;
  94.                                     System.out.println("\t" + (ctr + 1) + "        " + qt);                        
  95.                                 }
  96.  
  97.                                 else if (cpub[ctr] >= 1 && cpub[ctr] < qt)
  98.                                 {
  99.                                     System.out.println("\t" + (ctr + 1) + "        " + cpub[ctr]);
  100.                                     cpub[ctr] = 0;
  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.                     }
  117.  
  118.             }
  119.     }
  120.  
  121.  

I'm getting this output, it display the error just after asking the jobs' CPU Bursts.. the time quantum isn't even shown..
Expand|Select|Wrap|Line Numbers
  1. --------------------Configuration: <Default>--------------------
  2. ROUND ROBIN SCHEDULING
  3.  
  4.  
  5. NOTE: Quantum Time to be entered 
  6.     must be higher than the lowest Job's CPU Burst Value 
  7.     and must be lower than the highest Job's CPU Burst Value
  8.  
  9.  
  10. Enter no. of Jobs from 1 to 10 only: 3
  11.  
  12. Enter CPU Burst of Job 1: 4
  13.  
  14. Enter CPU Burst of Job 2: 6
  15.  
  16. Enter CPU Burst of Job 3: 2
  17. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
  18.     at RoundRobin.main(RoundRobin.java:38)
  19.  
  20. Process completed.
  21.  
  22.  
please help!
Oct 12 '10 #1
1 1252
You are using the for/loop variable *ctr* outside of the loop.
Oct 12 '10 #2

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

Similar topics

0
by: Phillip Montgomery | last post by:
Hello all; I'm trying to debug an issue with a java script called, SelectSockets. It appears to be a fairly common one found on the web. I downloaded the SGI Java v1.4.1 installation from SGI's...
22
oll3i
by: oll3i | last post by:
i get Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Producent.main(producent.java:605) when i run it from bat @start "Supply Chain Management-Producer to...
3
by: Ananthu | last post by:
Hi This is my codings in order to access mysql database from java. Codings: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
9
by: tiyaramunna | last post by:
I am trying to configure my system with Java program just to practice on the coding....when i compile a test.java program i am able to see the class file but i cant run the program ... I am getting...
4
by: jmitch89 | last post by:
I don't why I get this error: Exception in thread "main" java.lang.NoClassDefFoundError The statement below works just fine: java -cp...
3
by: ohadr | last post by:
hi, i get Exception in thread "main" java.lang.NullPointerException when i run my application. the exact error is: "Exception in thread "main" java.lang.NullPointerException at...
1
by: onlinegear | last post by:
HI i am writing this for college i know i have loads of combo boxes with nothing in the i havent got that far yet. but every time i run this is comes up with this erro run: Exception in thread...
1
by: rajujrk | last post by:
Hai All, I am Having a problem in the following... import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.io.FileUtils; import...
1
by: shaikhussain | last post by:
public class EmployeeServicesTestCase { /** * @param args */ public static void main(String s)throws Exception { // TODO Auto-generated method stub BeanFactory beans=new...
1
by: Buena Velasco | last post by:
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. import...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.