j'ai créé une méthode qui lit un fichier comme cela (... // sont des commentaires, les ignorer):StringTokenizer NoSuchElements
5 // n jobs
2 // n tools
1 4 5 6 2
1 5 4 2 3
La matrice représente les outils utilisés pour chaque travail, mais il n » Peu importe vraiment ici.
Voici la méthode:
public static JobS inputJobMatrix(){
String line = ""; // Line in tokenizer
int jobN = inputJobN(); //First number of the file (jobs) works
int toolN = inputToolN(); //Second number of the file (tools) works
//Instancing JobS object
JobS inputJobS = new JobS(jobN, toolN);
int[][] tabFill = new int[jobN][toolN];
int[] tabFillOrder = new int[jobN];
try {
// Initializing reader.
FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
StringTokenizer st = new StringTokenizer(line);
for(int i=0; i<3; i++){ //ReachFirstLine of matrix
line = br.readLine();
//System.out.println(line);
}
//Instancing tab for Job Order 1...n
int[] a = new int[jobN];
for (int i=0; i<jobN; i++){
a[i]=i+1;
}
//Filling Order tab with Job order
JobS.fillLine(tabFillOrder, a, 0); //Fills the tab with the tab a (make a copy of it we could say)
//Reading the matrix line by line and filling tab line
for(int i=0; i<jobN; i++){
for(int j=0; j<toolN; j++){
String str = st.nextToken();
System.out.println(str);
tabFill[i][j] = Integer.parseInt(str);
}
line = br.readLine();
}
inputJobS.setJobS(tabFill);
br.close();
} catch (IOException e) {
System.out.println("File not found exception in inputJobMatrix.");
}
return inputJobS;
}
qui se traduit par:
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at pProgra.ReadJobS.inputJobMatrix(ReadJobS.java:84)
at pProgra.PPMain.main(PPMain.java:14)
J'ai essayé de chercher des problèmes dans mes boucles, mais na pas trouver, et je ne comprends pas pourquoi il ne fonctionne pas. Le but ici est de remplir un tableau int bidimensionnel avec la matrice du fichier d'entrée (par exemple celui que j'ai donné précédemment avec des jobs et des outils) et d'utiliser ce tableau pour mon objet (JobS, je vais donner le constructeur ici aussi si elle peut aider):
public class JobS {
private int[] jobOrder;
private int[][] jobS;
public JobS(int jobs, int tools){// Creates one more line for the title (jobOrder).
super();
int[][] tab = new int[jobs][tools];
int[] tab2 = new int[jobs];
this.jobS = tab;
this.jobOrder = tab2;
}
Et le poseur j'utilise à la fin:
public void setJobS(int[][] jobS) {
this.jobS = jobS;
}
I Tried détaillant le code autant que possible avec des commentaires, je l'espère, vous comprendrez ce que je veux faire.
C'est la première fois que j'essaie de faire une application "complexe" alors peut-être que je suis juste stupide et j'ai oublié quelque chose, mais maintenant je cherche depuis une heure et n'ai toujours aucune idée de ce qui cause ceci ..
J'espère que vous pouvez aider, merci d'avance! LL
'StringTokenizer st = new StringTokenizer (line); 'à ce moment là' line' a la valeur '" "'. Il n'y a pas d'éléments. Déplacez cette ligne de code après votre boucle 'for'. – jlordo