473,786 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception in thread "main" java.lang.Array IndexOutOfBound sException:

I'm having trouble finding the error on my code. I know what "Exception thread ... ArrayIndexOutOf Bounds... " 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 1863
improvcornartist
303 Recognized Expert Contributor
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
3012
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 webpage and installed it using SGI's swmgr application. The installation was very straight forward and there were no errors when I installed the package. Then I ran /usr/java2/bin/javac SelectSockets.java to make the SelectSockets.class file.
22
41043
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 Queue" run Producent queue1 queue2 queue3 queue4 maybe you will se something i dont see
1
1790
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 com.inventive.StockMarketTrading; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent;
3
6445
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
3286
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 the following error Exception in thread "main" java.lang.NoClassDefFoundError: test Caused by: java.lang.ClassNotFoundException: test at java.net.URLClassLoader$1.run(Unknown Source) at...
4
14733
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 "appframework-1.0.3.jar;swing-worker-1.1.jar";CurrentStrobe.jar com.visionpro.currentstrobe.CurrentStrobeApp However, the statement below produces the error: java -cp "appframework-1.0.3.jar;swing-worker-1.1.jar" -jar CurrentStrobe.jar Exception in thread "main"...
3
7661
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 sortmergejoin.MergeJoin.Field(MergeJoin.java:204) at sortmergejoin.MergeJoin.SMJoin(MergeJoin.java:84) at sortmergejoin.MergeJoin.<init>(MergeJoin.java:34) at sortmergejoin.Main.main(Main.java:24) Java Result: 1"
1
6758
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 "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1041) at java.awt.Container.add(Container.java:365) at orderingsystem.OrderingSystem.<init>(OrderingSystem.java:261) at...
1
10101
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 java.io.IOException;
1
3723
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 XmlBeanFactory(new ClassPathResource("applicationContext.xml")); EmployeeService es=(EmployeeService)beans.getBean("employeeService");
0
9492
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10108
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6744
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.