473,419 Members | 1,795 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,419 software developers and data experts.

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

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
1 1844
improvcornartist
303 Expert 100+
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

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...
1
by: GHKASHYAP | last post by:
Hi this is the exception i am getting when i am trying to run this application: Exception in thread "main" java.lang.NullPointerException thanks in advance.. package...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.