I am trying to read lines from a configuration file and append each line into an ArrayList using the add() method.
However when I go to print the contents of the ArrayList through the use of a foreach, it only prints the last item to be entered. It seems to me like the add() method may not be appending properly? I also tried using a generic for loop instead of foreach and still the same result.
public static void interpret(String line){
ArrayList<String> rooms = new ArrayList<>();
ArrayList<String> rules = new ArrayList<>();
// Ignore Room and Rule templates
if(line.contains("(") && line.contains(")")){
System.out.println("skip");
return;
}
if(line.contains("Room;")){
rooms.add(line);
rooms.forEach(System.out::println);
}
if(line.contains("Rule;")){
rules.add(line);
rules.forEach(System.out::println);
}
}
The output of this is below.
Rule; (Room: SmartObject, state{condition}, state{condition}, ...)
skip
Rule; Garden: Sprinklers, on{time=15}, off{time=16}, off{weather="raining"}
Rule; Garden: Sprinklers, on{time=15}, off{time=16}, off{weather="raining"}
Rule; Kitchen: Coffee machine, on{time=6}, off{time=12}
Rule; Kitchen: Coffee machine, on{time=6}, off{time=12}
It is mixed in with the actual text lines from the file its reading but as you can see it only prints the line above it, which was the last line to be appended into the ArrayList.
It should look something like this.
Rule; (Room: SmartObject, state{condition}, state{condition}, ...)
skip
Rule; Garden: Sprinklers, on{time=15}, off{time=16}, off{weather="raining"}
Rule; Garden: Sprinklers, on{time=15}, off{time=16}, off{weather="raining"}
Rule; Kitchen: Coffee machine, on{time=6}, off{time=12}
Rule; Garden: Sprinklers, on{time=15}, off{time=16}, off{weather="raining"}
Rule; Kitchen: Coffee machine, on{time=6}, off{time=12}
Any help/insight would be greatly appreciated.