Java Scanner没有得到所有的代码

时间:2013-11-21 18:51:45

标签: 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.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)并打印它并且它没有问题。

2 个答案:

答案 0 :(得分:1)

您的代码是否可能抛出非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 {
    ...

答案 1 :(得分:1)

我在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);
        }