0

My program stimulates FCFS scheduling algorithm. It takes a .csv file as input and output the average waiting time. I have trouble with inputting the file. This is the error that i get when i ran the code:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
     at main.FCFS.main(FCFS.java:16)

What am I doing wrong? I cannot seems to figure it out. Please help.

package main;

  //programming FCFS scheduling algorithm

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;

public class FCFS {
   public static void main(String[] args) throws FileNotFoundException {
    // To Store Name of the file to be opened
    String file = args[0];
    int i = 0, n;
    double AWT = 0, ATT = 0;
    int AT[] = new int[100];
    int BT[] = new int[100];
    int WT[] = new int[100];
    int TAT[] = new int[100];
    int PID[] = new int[100];
    // To open file in read mode
    FileInputStream fin = null;

    // To read input(file name) from standard input stream
    Scanner s = new Scanner(new File("/Users/SLO/ex.csv"));

    // To hold each single record obtained from CSV file
    String oneRecord = "";

    try {
        // Open the CSV file for reading
        fin = new FileInputStream(file);

        // To read from CSV file
        s = new Scanner(fin);

        // Loop until all the records in CSV file are read
        while (s.hasNextLine()) {

            oneRecord = s.nextLine();

            // Split record into fields using comma as separator
            String[] details = oneRecord.split(",");
            PID[i] = Integer.parseInt(details[0]);
            AT[i] = Integer.parseInt(details[1]);
            BT[i] = Integer.parseInt(details[2]);
            System.out.printf("Process Id=%d\tArrival Time=%d\tBurst Time=%d\n", PID[i], AT[i], BT[i]);
            i++;
        }
        WT[0] = 0;
        for (n = 1; n < i; n++) {
            WT[n] = WT[n - 1] + BT[n - 1];
            WT[n] = WT[n] - AT[n];
        }
        for (n = 0; n < i; n++) {
            TAT[n] = WT[n] + BT[n];
            AWT = AWT + WT[n];
            ATT = ATT + TAT[n];
        }
        System.out.println(" PROCESS BT WT TAT ");
        for (n = 0; n < i; n++) {
            System.out.println(" " + PID[n] + " " + BT[n] + " " + WT[n] + " " + TAT[n]);
        }
        System.out.println("Avg waiting time=" + AWT / i);
        System.out.println("Avg waiting time=" + ATT / i);

    } catch (FileNotFoundException e) {
        System.out.printf("There is no CSV file with the name %s", file);
    }

    finally {
        if (fin != null) {
            try {
                fin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 }
}
4
  • Can you show the CSV file? Commented Apr 15, 2017 at 3:33
  • In your code, what is line 16 and how do you run the program i.e. how are you supplying file argument to program? Commented Apr 15, 2017 at 3:46
  • the file has 3 columns of numbers like this: 1,4,45 Commented Apr 15, 2017 at 14:58
  • At first i tried to supply the input file using terminal. Now i am using Elipse Commented Apr 15, 2017 at 15:01

1 Answer 1

1

Well, an ArrayIndexOutOfBoundsException is thrown if there are no arguments, because you access the empty array at a non existing index. Add the following lines to check if the argument is passed correctly:

...
public static void main(String[] args) throws FileNotFoundException {
    if (args.length == 0)
       throw new IllegalArgumentException("Missing mandatory file name in argument list");
    // To Store Name of the file to be opened
    String file = args[0];
...

If the missing argument ist the reason for the failure, check out https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html to find out how to pass it properly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.