A Serious Case of For-Each Loop Confusion

Viewed 151

I have a program that extracts data from a text file and stores it in two different arrays like this:

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());
        }
  }

I want to take the first element from each array and create an Event with it. I've had some success by creating the events and getting them to print to the console, but they should be printing to the console with a staggered delay based on the "l" value that is being passed into the method createSpecificEvent(). The events print to screen instantly, so I think my for-each loop is wrong and it's passing a "0" into each createSpecificEvent() instance.

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

Here is what my createSpecificEvent() code looks like:

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;
}

Here is what my text file looks like:

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

Here is what the output looks like:

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

The first line should print instantly and the last line should print after 20 seconds.

2 Answers
Related