0

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.

2 Answers 2

1

Here is the problem:

ArrayList<String> rules = new ArrayList<>();

you are creating a new ArrayList each time, instead of adding to an existing one

Suggestion:

  1. Pass in the existing arraylist to your method OR
  2. Declare a member variable at the class level
Sign up to request clarification or add additional context in comments.

1 Comment

Oh of course, my apologies i should have seen that sooner. Thank you for your time.
0

Use the following method singature:

public static void interpret(List<String> rooms, List<String> rules, String line){

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

}

Instead of creating the lists every time this function is called, create the lists in the caller function and pass to this method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.