每个循环混乱的严重案例

时间:2013-12-11 05:43:49

标签: java loops foreach

我有一个程序从文本文件中提取数据并将其存储在两个不同的数组中:

public void extractDataIntoArray() {
    array = new ArrayList<String>();    
    while (s.hasNext()) {
        s.useDelimiter("[^0-9]+");
        array.add(s.next());
    }
  }

  public void extractDataIntoArray2() {
        array2 = new ArrayList<String>();   
        while (s.hasNext()) {
            s.useDelimiter("Event=|,(.)+[\\r\\n]*Event=|,(.)+[\\r\\n]*");
            array2.add(s.next());
        }
  }

我想从每个数组中获取第一个元素并使用它创建一个Event。我通过创建事件并将它们打印到控制台获得了一些成功,但它们应该根据传递给方法createSpecificEvent()的“l”值以交错延迟打印到控制台。 。事件立即打印到屏幕,所以我认为我的for-each循环是错误的,它将“0”传递给每个createSpecificEvent()实例。

public void action() {
    openFile();                     // grab file with Scanner
    extractDataIntoArray();         // extract values from text value and put in array

    for (String  e : array) {              
        long l = Long.parseLong(e); // for each value in array, convert to long and set to l

        openFile();                     // grab file with Scanner...
        extractDataIntoArray2();        // extract values from text file and put in array2

        for (String  e2 : array2) {
            event = createSpecificEvent(e2, l);
            //System.out.println(event);
            addEvent(event);
        }
    }
}

以下是我的createSpecificEvent()代码:

public Event createSpecificEvent(String eventName, long delayTime) {
    Event event = null;

    switch(eventName) {
    case "ThermostatNight": 
        event = new ThermostatNight(delayTime);
        break;
    case "ThermostatDay": 
        event = new ThermostatDay(delayTime);
        break;
    case "LightOn": 
        event = new LightOn(delayTime);
        break;
    case "LightOff": 
        event = new LightOff(delayTime);
        break;
    case "WaterOn":
        event = new WaterOn(delayTime);
        break;
    case "WaterOff": 
        event = new WaterOff(delayTime);
        break;
    case "Bell": 
        event = new Bell(delayTime);
        break;
    case "FansOn": 
        event = new FansOn(delayTime);
        break;
    case "FansOff": 
        event = new FansOff(delayTime);
        break;
    case "Terminate": 
        event = new Terminate(delayTime);
        break;
    }

    return event;
}

以下是我的文本文件:

Event=ThermostatNight,time=0
Event=LightOn,time=2000
Event=WaterOff,time=10000
Event=ThermostatDay,time=12000
Event=Bell,time=9000,rings=5
Event=WaterOn,time=6000
Event=LightOff,time=4000
Event=Terminate,time=20000
Event=FansOn,time=7000
Event=WindowMalfunction,time=15000
Event=FansOff,time=8000

以下是输出结果:

Restarting system
Thermostat on night setting
Light is on
Greenhouse water is off
Thermostat on day setting
Bing!
Greenhouse water is on
Light is off
Terminating

第一行应立即打印,最后一行应在20秒后打印。

2 个答案:

答案 0 :(得分:4)

 public void action() {

 openFile();                     // grab file with Scanner
 extractDataIntoArray();         // extract values from text value and put in array

 openFile();                     // grab file with Scanner...
 extractDataIntoArray2();        // extract values from text file and put in array2

 for(int i = 0; i < array.size() ;i++)
 {
    long l = Long.parseLong(array.get(i)); 

    string e2 = array2.get(i);
    event = createSpecificEvent(e2, l);
    addEvent(event);

    if(e2.equals("Terminate"))
     {
       break;
     }
 }
}

答案 1 :(得分:1)

你正在另一个循环中运行。

您应该首先运行extractDataIntoArray()extractDataIntoArray2(),然后在其中一个数组上运行循环,并在单个循环中使用这两个值,如T-D所示。

您遇到的问题很可能是实施数据提取方法。如果它们从同一个文件中提取,它们实际上应该是一种方法,您只需要打开/读取一次该文件。因为它们似乎正在生产不同长度的阵列!