[英]Java Scanner does not get all the lines
我有以下方法從文本獲取輸入,然后它應該逐行存儲:
public static MDVRPData parse(String myFile) {
MDVRPData myData = new MDVRPData(0, 0, 0, null, null);
try {
Scanner s = new Scanner(new FileReader(myFile));
int currentLine = 1;
String line = s.nextLine();
String delims = "[ ]+";
String[] tokens = line.split(delims);
if (Integer.parseInt(tokens[0]) != 2) {
System.out.println("The data might not be appropriate.");
}
myData.setNrVehicle(Integer.parseInt(tokens[1]));
myData.setNrCustomers(Integer.parseInt(tokens[2]));
myData.setNrDepots(Integer.parseInt(tokens[3]));
DepotData[] dData = new DepotData[myData.getNrDepots()];
CustomerData[] cData = new CustomerData[myData.getNrCustomers()];
while (s.hasNextLine()) {
line = s.nextLine();
delims = "[ ]+";
tokens = line.split(delims);
currentLine++;
for (int i = 0; i < tokens.length; i++) {
System.out.print(tokens[i] + ' ');
}
System.out.println(currentLine);
if (currentLine <= myData.getNrDepots() + 1) {
dData[currentLine - 2].setMaxDuration(Integer.parseInt(tokens[0]));
dData[currentLine - 2].setMaxLoad(Integer.parseInt(tokens[1]));
}
if ((currentLine > myData.getNrDepots() + 1) && (currentLine <= 1 + myData.getNrDepots() + myData.getNrCustomers())) {
cData[currentLine - 1 - myData.getNrDepots()].setNumber(Integer.parseInt(tokens[0]));
cData[currentLine - 1 - myData.getNrDepots()].setxCoord(Integer.parseInt(tokens[1]));
cData[currentLine - 1 - myData.getNrDepots()].setyCoord(Integer.parseInt(tokens[2]));
cData[currentLine - 1 - myData.getNrDepots()].setServiceDuration(Integer.parseInt(tokens[3]));
cData[currentLine - 1 - myData.getNrDepots()].setDemand(Integer.parseInt(tokens[4]));
cData[currentLine - 1 - myData.getNrDepots()].setFrequencyOfVisit(Integer.parseInt(tokens[5]));
cData[currentLine - 1 - myData.getNrDepots()].setVisitCombNr(Integer.parseInt(tokens[6]));
int j;
int[] temp = new int[Integer.parseInt(tokens[6])];
for (j = 0; j < temp.length; j++) {
temp[j] = Integer.parseInt(tokens[7 + j]);
}
cData[currentLine - 1 - myData.getNrDepots()].setVisitCombList(temp);
}
if (currentLine > myData.getNrCustomers() + myData.getNrDepots() + 1) {
dData[currentLine - myData.getNrCustomers() + myData.getNrDepots() + 2].setNumber(Integer.parseInt(tokens[0]));
dData[currentLine - myData.getNrCustomers() + myData.getNrDepots() + 2].setxCoord(Integer.parseInt(tokens[1]));
dData[currentLine - myData.getNrCustomers() + myData.getNrDepots() + 2].setyCoord(Integer.parseInt(tokens[2]));
}
}
myData.setCustomerData(cData);
myData.setDepotData(dData);
} catch (IOException ex) {
ex.printStackTrace(); // for now, simply output it.
} finally {
return myData;
}
}
這是輸入文本的一部分:
2 2 50 4
0 160
0 160
0 160
0 160
1 37 52 0 7 1 4 1 2 4 8
2 49 49 0 30 1 4 1 2 4 8
3 52 64 0 16 1 4 1 2 4 8
4 20 26 0 9 1 4 1 2 4 8
5 40 30 0 21 1 4 1 2 4 8
6 21 47 0 15 1 4 1 2 4 8
由於某種原因,代碼讀取第一行正確,進入while循環,讀取第二行正確並停止。 在實現這個方法之前,我寫了一個只是逐行獲取文件然后拆分它的String line = s.nextLine(); String delims ="[ ]+"; String[] tokens = line.split(delims)
String line = s.nextLine(); String delims ="[ ]+"; String[] tokens = line.split(delims)
String line = s.nextLine(); String delims ="[ ]+"; String[] tokens = line.split(delims)
並打印它並且它沒有問題。
您的代碼是否可能拋出非IOException? 如果是這樣,那么您的應用程序將默默地吞下錯誤並返回到目前為止讀取的行。
例如,在下面設計的代碼片段中,即使拋出RuntimeException,也只會忽略它:
try {
int i = 0;
if (i == 1) {
throw new IOException("wont be thrown");
}
throw new RuntimeException("boom");
} catch(IOException e) {
System.out.println("Got an exception");
} finally {
return "a result";
}
將返回“結果”,並且調用代碼將不再明智。
要檢查,我建議添加一個“catch all”塊來驗證:
...
} catch(IOException e) {
System.out.println("Got an exception");
} catch(Throwable te) {
System.out.println("Got another exception");
} finally {
...
我在Sean Landsman的幫助下找到了我的錯誤。 我在IOException之后添加了一個catch:
} catch (Throwable e) {
System.out.println("Got another exception");
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
e.printStackTrace(pw);
System.out.println(sw.getBuffer().toString());
}
我得到了NullPointerException,因為我在創建數組后沒有初始化我的對象。 我通過添加以下代碼擺脫了錯誤:
DepotData[] dData = new DepotData[myData.getNrDepots()];
for (int i = 0; i < myData.getNrDepots(); i++) {
dData[i] = new DepotData(0, 0, 0, 0, 0);
}
CustomerData[] cData = new CustomerData[myData.getNrCustomers()];
for (int i = 0; i < myData.getNrCustomers(); i++) {
cData[i]= new CustomerData(0, 0,0,0,0,0,0,null);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.