Home > Mobile >  getting ArrayIndexOutBoundException
getting ArrayIndexOutBoundException

Time:11-19

i am very new to programming. I am implementing FirstComeFirstServe(FCFS) Scheduling. I am getting array index out of bound exception while entering first input in ArrivalTime array. It can be a silly mistake, but I cannot figure it out, Please help me, Thank You for your time.

import java.util.Scanner;

public class FCFS_Practice {
    public static Scanner scanner = new Scanner(System.in);
    static int numberOfProcess;
    int[]ProcessID = new int[numberOfProcess];
    int[]ArrivalTime  = new int[numberOfProcess];
    int[]BurstTime = new int[numberOfProcess];
    int[]CompletionTime = new int[numberOfProcess];
    int[]TurnAroundTime = new int[numberOfProcess];
    int[]WaitingTime = new int[numberOfProcess];
    float avgWaitingTime,avgTurnAroundTime;

    public void takeInput(){
        System.out.println("Enter the number of process: ");
        numberOfProcess = scanner.nextInt();
    }
    public void inputArrivalAndBurstTime(){
        for(int i = 0;i < numberOfProcess;i  ){
            System.out.printf("Enter Arrival Time for Process %d: ",i 1);
            ArrivalTime[i] = scanner.nextInt();
            scanner.nextLine(); // Buffer Flush
            System.out.printf("Enter Burst Time for Process %d: ",i 1);
            BurstTime[i] = scanner.nextInt();
            scanner.nextLine(); // buffer flush
            ProcessID[i] = i 1;
        }
    }
    public void calculateCompletionTime(){
        for(int i = 0 ; i < numberOfProcess ; i  ){
            if(i==0){
                CompletionTime[0]=BurstTime[0];
            } else if (ArrivalTime[i]<CompletionTime[i-1]) {
                CompletionTime[i]=CompletionTime[i-1] BurstTime[i];
            } else {
                 CompletionTime[i]=ArrivalTime[i] BurstTime[i];
            }
        }
    }
    public void calculateTurnAroundAndWaitingTime(){
        for(int i = 0 ; i < numberOfProcess ; i  ){
            TurnAroundTime[i]=CompletionTime[i] ArrivalTime[i];
            WaitingTime[i]=TurnAroundTime[i]-BurstTime[i];
        }
    }
    public void getAvgWaitingTimeAndAvgTurnAroundTime(){
        for(int i = 0 ; i<numberOfProcess;i  ){
            avgTurnAroundTime =TurnAroundTime[i];
            avgWaitingTime =WaitingTime[i];
        }
        avgWaitingTime = avgWaitingTime/numberOfProcess;
        avgTurnAroundTime = avgTurnAroundTime/numberOfProcess;
    }
    public void getTable(){
        System.out.println("ProcessNo.  ArrivalTime  BurstTime  CompletionTime  TurnAroundTime  WaitingTime");
        for(int i = 0 ; i < numberOfProcess ; i  ){
            System.out.println(ProcessID[i] "\t\t" ArrivalTime[i] "\t\t" BurstTime[i] "\t\t" CompletionTime[i] "\t\t\t" TurnAroundTime[i] "\t\t\t" WaitingTime[i]);
        }
    scanner.close();
    }
}

OutPut

CodePudding user response:

The problem with your code is that you initialize arrays of int (ProcessID..) with the static int numberOfProcess - which initialized by default as 0.

You update it from Scanner at takeInput method but length of the arrays have been already initialized as 0 length.

You have to work with the initialization process to set initial arrays values only after you get your numberOfProcesses variable value from Scanner. You can read more about default initialization at Java Official Documentation.

As fast workaround for learning purpose just change default value. But remember that error returns if numberOfProcess will be higher, so to modify code is mandatory for solid code practice and passing of automation tests.

    static int numberOfProcess = 10;

As more solid (but not a very nice one solution) initialize your arrays only after you know the correct value of numberOfProcess variable:

    int numberOfProcess;
    int[]ProcessID;
    int[]ArrivalTime;
    int[]BurstTime;
    int[]CompletionTime;
    int[]TurnAroundTime;
    int[]WaitingTime;


    public void takeInput(){
        System.out.println("Enter the number of process: ");
        numberOfProcess = scanner.nextInt();
        ProcessID = new int[numberOfProcess];
        ArrivalTime  = new int[numberOfProcess];
        BurstTime = new int[numberOfProcess];
        CompletionTime = new int[numberOfProcess];
        TurnAroundTime = new int[numberOfProcess];
        WaitingTime = new int[numberOfProcess];
    }

If you use ArrayList instead of []int you don't need to set array length. However, you have also to learn deep the difference between ArrayList (resizable array) and array - there are tasks where array is preferable.

  • Related