Home > Mobile >  I am very new to programming. I am implementing First Come First Serve(FCFS)
I am very new to programming. I am implementing First Come First Serve(FCFS)

Time:11-19

I am very new to programming and to stackOverflow. I am implementing First Come First Serve(FCFS).But I am getting array index out of bound exception, while entering input in []ArrivalTime I can be a silly mistake but i can not figure out why ,Please help me

import java.util.Scanner;

public class FCFS_Practice {
    public static Scanner scanner = new Scanner(System.in);
    static int numberOfProcess = 0;
    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 = 0,avgTurnAroundTime = 0;

    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](https://i.stack.imgur.com/z1CEd.png)

CodePudding user response:

You asked

Why getting IndexOutOfBoundsException with in shared snippet.

You declared static int numberOfProcess = 0; and creating many arrays of length 0, so every time you try to access those array you will get IndexOutOfBoundsException which thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.

Workaround ?

  • Many ways to resolve it, one practice is to declare your array implementation size after reading numberOfProcess through your scanner and make their pointer static.
    public static void main(String[] args) {
        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];
        ...
    }
    static int[] processID;
    static int[] arrivalTime;
    static int[] burstTime;
    static int[] completionTime;
    static int[] turnAroundTime;
    static int[] waitingTime;
    static Scanner scanner = new Scanner(System.in);
    ...
  • Other practice is just change the declaration type to list and traverse/allocate values accordingly.

Extra point

  • Please follow language model standards as well, like naming convention

CodePudding user response:

Ordinary Array can not be resized once declared. So use ArrayList Instead

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class FCFS_Practice {
    public static Scanner scanner = new Scanner(System.in);
    static int numberOfProcess = 0;
    ArrayList<Integer> processID = new ArrayList();
    ArrayList<Integer> arrivalTime = new ArrayList();
    ArrayList<Integer> burstTime = new ArrayList();
    ArrayList<Integer> completionTime = new ArrayList();
    ArrayList<Integer> turnAroundTime = new ArrayList();
    ArrayList<Integer> waitingTime = new ArrayList();
    
   
   
    float avgWaitingTime = 0,avgTurnAroundTime = 0;

    public static void main(String[] args) throws IOException
      {
          FCFS_Practice practice = new FCFS_Practice();
          practice.takeInput();
          practice.initializeArrayList();
          practice.inputArrivalAndBurstTime();
          practice.calculateCompletionTime();
          practice.calculateTurnAroundAndWaitingTime();
          practice.getTable();
          
      }
    public void initializeArrayList()
    {
        for(int i=0; i < numberOfProcess;i  )
        {
            processID.add(0);
    arrivalTime.add(0);
    burstTime.add(0);
     completionTime.add(0);
     turnAroundTime.add(0);
     waitingTime.add(0);
        }
    }
    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.set(i,  scanner.nextInt());
            scanner.nextLine(); // Buffer Flush
            System.out.printf("Enter Burst Time for Process %d: ",i 1);
            burstTime.set(i, scanner.nextInt());
            scanner.nextLine(); // Buffer flush
            processID.set(i, i 1);
        }
    }
    public void calculateCompletionTime(){
        for(int i = 0 ; i < numberOfProcess ; i  ){
            if(i==0){
                completionTime.set(0,burstTime.get(0));
            } else if (arrivalTime.get(i)<completionTime.get(i-1)) {
                completionTime.set(i,completionTime.get(i-1) burstTime.get(i));
            } else {
                 completionTime.set(i, arrivalTime.get(i) burstTime.get(i));
            }
        }
    }
    public void calculateTurnAroundAndWaitingTime(){
        for(int i = 0 ; i < numberOfProcess ; i  ){
            turnAroundTime.set(i,completionTime.get(i) arrivalTime.get(i));
            waitingTime.set(i,turnAroundTime.get(i)-burstTime.get(i));
        }
    }
    public void getAvgWaitingTimeAndAvgTurnAroundTime(){
        for(int i = 0 ; i<numberOfProcess;i  ){
            avgTurnAroundTime =turnAroundTime.get(i);
            avgWaitingTime =waitingTime.get(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.get(i) "\t\t" arrivalTime.get(i) "\t\t" burstTime.get(i) "\t\t" completionTime.get(i) "\t\t\t" turnAroundTime.get(i) "\t\t\t" waitingTime.get(i));
        }
    scanner.close();
    }
}

You have started variable names with Upper case alphabet.. java coding conventions

I have changed variable names accordingly

ArrayList explained

  • Related