2017-04-15 53 views
0

我的程序刺激FCFS调度算法。它将一个.csv文件作为输入并输出平均等待时间。我无法输入文件。这是我运行代码时得到的错误:Java - .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(); 
      } 
     } 
    } 

} 
} 
+0

你能显示CSV文件吗? –

+0

在你的代码中,第16行是什么,以及如何运行程序,即如何为程序提供文件参数? –

+0

该文件有3列这样的数字:1,4,45 – begincoding123

回答

1

嘛,一个ArrayIndexOutOfBoundsException是,如果没有参数,因为你在一个不存在的索引来访问的空数组抛出。添加以下行来检查参数正确传递:

... 
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]; 
... 

如果缺少的参数IST失败的原因,检查出https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html找出如何正确地传递。