Java Scanner无法获取所有行
我使用以下方法从文本中获取输入,然后逐行存储:Java Scanner无法获取所有行,java,java.util.scanner,Java,Java.util.scanner,我使用以下方法从文本中获取输入,然后逐行存储: 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.
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;
}
}
出于某种原因,代码读取第一行正确,进入while循环,读取第二行正确,然后停止。在实现此方法之前,我编写了一个简单的方法,它只需逐行获取文件,然后将其拆分
String line=s.nextLine();字符串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 {
...
在肖恩·兰斯曼的帮助下,我设法找到了我的错误。我在IOException之后又添加了一个捕获:
} 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);
}
DepotData[]dData=新的DepotData[myData.getNrDepots()];
对于(int i=0;i
这可能是一个愚蠢的评论,但您是否检查了换行符?输入上的CR LF?是的,这是我检查的第一件事。我已经用第一个方法再次解析了输入,但它仍然有效。@AlexandruCimpanu如果要在WhiteSpace上拆分,请尝试使用delims asString delims=“\\s+”代码>看起来像是调试器的一个例子。应该很容易找出哪里出了问题。我不知道哪里会出问题,因为我只是在逐行输入的代码中添加了一些步骤。如果有帮助的话,我可以发布我开始的代码。我会在IOException之后再添加一个catch-我会更新我的代码段你是对的,有一个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);
}