返回空集,没有错误

时间:2015-05-09 01:40:41

标签: java set bufferedreader

我必须通读文件,获取行并将它们添加到自定义服务类,然后将该服务类添加到timeTable。然而,返回的timeTable是空的。

  • 假设文件格式正确

这是文件:

3
v1
1 v2 v3
2 v4
3

v2
1 
2 v1 v4
3

代码:

while ((line = buffReader.readLine()) != null) {

    if (firstLine){
        //Set session
        numberOfSessions = Integer.parseInt(line.trim());
        firstLine = false;

    } else if (line.trim().isEmpty()) {
        correctSession = 0;     
    } else if(correctSession < numberOfSessions + 1) {
        //Split the current line
        String[] splitLine = line.split(" ");

        //Check if it is the "Name" line
        if (correctSession == 0) {
            sourceName = line.trim(); 
            //Increase correct session
            correctSession++;
        } else { 
            //Add it to timetable as a service
            for (int i = 1;i < lineSplit.size();i++) {
                //Venue(String)
                Venue source = new Venue(sourceName);
                Venue destination = new Venue(splitLine[i]);

                //Service(venue, venue, int)
                Service service = new Service(source,destination,Integer.parseInt(splitLine[0]));

                timeTable.addService(service); /* This does not seem to be working*/
            }
            //Increase correct session
            correctSession++;
        }
    }           
}
//Close the reader
buffReader.close();

结果:

Actual: []
Expected: [Departs v1 after session 1 for v3, Departs v1 after session 2 for v4, Departs v1 after session 1 for v2, Departs v2 after session 2 for v4, Departs v2 after session 2 for v1]

1 个答案:

答案 0 :(得分:0)

除非我遗漏了某些东西,否则你似乎在迭代错误的东西(你的变量名非常相似)。但是这个,

for (int i = 1;i < lineSplit.size();i++) {

应该是(因为数组从0开始)类似

for (int i = 0;i < splitLine.length;i++) {

或者,使用The For-Each Loop之类的

for (String destinationName : splitLine) {
    //Venue(String)
    Venue source = new Venue(sourceName);
    // Venue destination = new Venue(splitLine[i]);
    Venue destination = new Venue(destinationName);