Java - .csv文件作为输入

时间:2017-04-15 03:20:30

标签: java csv input scheduling

我的程序激发了FCFS调度算法。它需要.csv文件作为输入并输出平均等待时间。我输入文件时遇到问题。这是我运行代码时得到的错误:

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

我做错了什么?我似乎无法弄明白。请帮忙。

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();
            }
        }
    }

 }
}

1 个答案:

答案 0 :(得分:1)

如果没有参数,则抛出ArrayIndexOutOfBoundsException,因为您在非现有索引处访问空数组。添加以下行以检查参数是否正确传递:

class Ctrl{
  constructor($scope, service1, service2){
    this.$scope = $scope;
    this.service1= service1;
    this.service2= service2;
  }
} 

如果缺失的参数是导致失败的原因,请查看https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html以了解如何正确传递。