【发布时间】:2017-08-23 13:29:31
【问题描述】:
我创建了一个读取这样的文件集的方法(//... 是 cmets,忽略它们):
5 // n jobs
2 // n tools
1 4 5 6 2
1 5 4 2 3
矩阵表示每个作业使用的工具,但在这里并不重要。
方法如下:
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;
}
这会导致:
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)
我尝试在我的循环中寻找问题,但没有找到任何问题,我不明白为什么它不起作用。 这里的目标是用输入文件的矩阵填充一个双向 int 数组(例如我之前用作业和工具给出的那个)并将该数组用于我的对象(JobS,我也会在这里给出构造函数如果有帮助的话):
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;
}
我最后使用的二传手:
public void setJobS(int[][] jobS) {
this.jobS = jobS;
}
我尝试使用 cmets 尽可能详细地编写代码,希望您能理解我想要做什么。
这是我第一次尝试做一个“复杂”的应用程序,所以也许我只是愚蠢并且忘记了一些东西,但现在我已经搜索了一个小时,仍然不知道是什么原因造成的..
希望您能提供帮助,在此先感谢! L.L.
【问题讨论】:
-
StringTokenizer st = new StringTokenizer(line);在那个时间点line的值是""。没有元素。在for循环之后移动这行代码。
标签: java file multidimensional-array stringtokenizer reader