2017-04-15 2 views
0

Mon programme stimule l'algorithme d'ordonnancement FCFS. Il prend un fichier .csv en entrée et affiche le temps d'attente moyen. J'ai des problèmes avec la saisie du fichier. C'est l'erreur que je reçois quand j'ai couru le code:Java - Fichier .csv en entrée

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

Qu'est-ce que je fais mal? Je ne peux pas le comprendre. S'il vous plaît aider.

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

Pouvez-vous afficher le fichier CSV? –

+0

Dans votre code, qu'est-ce que la ligne 16 et comment exécutez-vous le programme, c'est-à-dire comment fournissez-vous un argument de fichier au programme? –

+0

le fichier a 3 colonnes de nombres comme ceci: 1,4,45 – begincoding123

Répondre

1

Eh bien, un ArrayIndexOutOfBoundsException est levée s'il n'y a pas d'arguments, parce que vous accédez au tableau vide à un index non existant. Ajoutez les lignes suivantes pour vérifier si l'argument est transmis correctement:

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

Si l'argument manquant ist la raison de l'échec, consultez https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html pour savoir comment passer correctement.