Ciao a tutti ragazzi!!
Devo importare i dati di un file csv in una matrice e i dati di un altro file in un array in java, ma non credo di averlo fatto correttamente.
Un esempio dei file sono i seguenti:
1. x,1,2,3,4,..
1, x11, x12, x13,
2, x21, x22,...
..., ...
2. x, y
1, y1
2, y2
...,...
Questo e' quello che ho scritto (per la classe matrice):
codice:private int[][] matrix; public DistanceMatrix(int nFacilities, int nLocations) { this.matrix = new int[nFacilities][nLocations]; } // constructor public void loadFromFile(String fileName) throws FileNotFoundException { try{ Scanner s = new Scanner(fileName); s.next(); // Reading out the first line because it's not useful int row=1; // Starting from the second one cause the first one is not useful while (s.hasNext()){ String[] val = s.nextLine().split(","); // Reading data after "," for (int column=1;column<val.length;column++){ this.matrix[row][column]=Integer.parseInt(val[column]); } row++;} } catch (Exception e){}
Questo invece e' quello che ho scritto per importare i dati del secondo file in un array:
codice:... this.nFacilities = nFacilities; ... public void loadFacilitiesFromFile(String fileName) { fcs = new Facility[nFacilities]; int[] string = null; try{ Scanner s = new Scanner (new File(fileName)); s.next(); // Reading out the first line because it's not useful int row=1; // Starting from the second one cause the first one is not useful while (s.hasNext()){ String[] val = s.nextLine().split(","); // Reading data after "," string[row]=Integer.parseInt(val[1]); fcs[row]= new Facility(string[row]); row++;} } catch (Exception e){}
Potete gentilmente controllare se ho scritto giusto e dove stia sbagliando, perche sicuramente sto sbagliando qualcosa
Ringrazio vivamente tutti quelli che mi daranno una mano, che sono veramente disperato e non so piu dove sbattere la testa per sto programma!!
Saluti


Rispondi quotando


