0

The project requires me to create an ArrayList of objects and then print the complete list. Additionally the maximum amount of characters cannot exceed 280. The objects created are defined in my "Message" class and the lists are defined and handled in my MessagingService class. I cant wrap my head around why the MessagingService class cannot return the complete ArrayList that is defined. It simply returns nothing. I have added two other printing methods to test whether it is successfully added to the list.

The Message class:

import java.util.Objects;

public class Message {

    private String sender;
    private String content;

    public Message(String sender, String content) {
        this.sender = sender;
        this.content = content;
    }

    public String getSender() {
        return sender;
    }

    public String getContent() {
        return content;
    }

    public String toString() {
        return this.sender + ": " + this.content;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Message other = (Message) obj;
        if (!Objects.equals(this.sender, other.sender)) {
            return false;
        }
        if (!Objects.equals(this.content, other.content)) {
            return false;
        }
        return true;
    }

}

The MessagingService class:

import java.util.ArrayList;

public class MessagingService {

    private ArrayList<Message> messages;

    public MessagingService() {
        this.messages = new ArrayList<>();
    }

    public void add(Message message) {
        if (message.toString().length() <= 280) {
            this.messages.add(message);
        }
    }

    public ArrayList<Message> getMessages() {
        System.out.println("For each print method:");
        for (Message message : messages) {
            System.out.println(message);
        }
        System.out.println("");

        System.out.println("Println complete list method:");
        System.out.println(this.messages);
        System.out.println("");

        System.out.println("Return print method:");
        return this.messages;
    }
}

The Main class:

public class Main {

    public static void main(String[] args) {
        Message test1 = new Message("pieterer", "aaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbb");
        Message test2 = new Message("koos", "kaaskril");
        Message test3 = new Message("jan", "wikkelspies");
        MessagingService list = new MessagingService();
        System.out.println("");
        list.add(test1);
        list.add(test2);
        list.add(test3);
        list.getMessages();
    }
}

The link to the exercise can be found here: https://java-programming.mooc.fi/part-6/1-objects-within-objects

  • And the question is ? Sounds like an assignment or code review... – CodeScale Apr 26 '20 at 10:27
  • Can you be more specific on what is not working? Does the getMessages() method return an empy ArrayList or what? – dreameral Apr 26 '20 at 10:29
  • I ran your code and its printing as it should be. What you want to do after returning messages, you are not storing/using it anywhere – Shubham Apr 26 '20 at 10:35
  • I apologize for not being more specific. When uploading the code to the Moocfi server, it states: When one message containing 280 characters has been added to the messaging service, the getMessages method of MessagingService should return a list containing a single message. expected:<1> but was:<0> When called, the method public ArrayList getMessages() should only return the ArrayList, all other code in that method is my testing. My question is why isn't it returning anything? – Stephan Botes Apr 26 '20 at 10:38
  • it prints `hi For each print method: pieterer: aaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbb koos: kaaskril jan: wikkelspies Println complete list method: [pieterer: aaaaaaaaaa111...., koos: kaaskril, jan: wikkelspies] Return print method` what did you expect – Abhinav Chauhan Apr 26 '20 at 10:39
  • It is exactly as @Shubham says, the getMessages() method returns empty array list. But AFAIK it was added already via add() method. – Stephan Botes Apr 26 '20 at 10:42
  • @StephanBotes, I think you misinterpreted my comment. Your code is printing the value. And your getMessages method is also returning the list (with values). Now, my question was : What do you want to do with that list after returning it from getMessages method. Do you want to print it or something else ? – Shubham Apr 26 '20 at 10:47
  • The exercise instrucitons state that I should include: public ArrayList getMessages() to: returns the messages added to the messaging service. I thought I did so yet the upload error states that an output is expected. Isn't the test1, 2 and 3 objects then saved to the list already? I dont wish to print anything els, just return the newly updated list. – Stephan Botes Apr 26 '20 at 10:52

2 Answers2

0

Here, MessagingService is returning the list but you are not printing it in the main(). Replace line list.getMessages() in main() with System.out.println(list.getMessages()).

Also you can replace:

public ArrayList<Message> getMessages() {
        System.out.println("For each print method:");
        for (Message message : messages) {
            System.out.println(message);
        }
        System.out.println("");

        System.out.println("Println complete list method:");
        System.out.println(this.messages);
        System.out.println("");

        System.out.println("Return print method:");
        return this.messages;
    }

with below code:

@Override
public String toString() {
    return new ReflectionToStringBuilder(this,ToStringStyle.SHORT_PREFIX_STYLE).toString();}
Anu
  • 80
  • 1
  • 2
  • 13
0

I found the solution: In add() method, the line was: if(message.toString().length() <= 280) where it should be: if(message.getContent().length() <= 280)