Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java LinkedList problem

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java LinkedList problem

    Hello!

    I have a program which resembles an online forum. It has message chains, messaged and messages can be replied. I have 2 classes: Chain and Message classes. Messages can be replies so message class has an LinkedList and if message gets replies, reply goes to list (a message can only reply to one message). Chain class also has LinkedList and it contains all messages, which don't have any replies. If new message gets added, it goes to list in chain class and if that message gets replied, that message goes to list in message class.
    I have a small problem when I'm trying to add replies to messages using a method in Chain class. This problem does not occur if I do the same using Message class. Firstly, when I add new message to Chain, it goes to the list without problem and if I try to reply that message, it works as well. However, if I'm trying to reply a "reply" (message in message class LinkedList), it doesn't work. My program thinks that I'm trying to reply to the second message, which is reply to the first one.

    I need this output:

    message1
    --message2 reply to message 1
    ----message3 reply to message 2
    ------message4 reply to message 3
    ----message5 reply to message 2
    message6

    lines stand for whitespace. Every reply gets indent of "--" and if reply is replies, "----" and so on.

    Im currently getting this:

    message1
    --message2 reply to message 1
    ----message3 reply to message 2
    ----message4 reply to message 3
    message6

    Something goes wrong when I'm adding replies to messages, since my program can print properly 2 replies, so indent of "----", but after that it doesn't work.

    My program:
    public class Chain(
        private LinkedList messages;
     
        public void addReplyToMessage(int id, String comment){
           // if there are messages in chain
     
            if (getMessages().size() > 0){
     
                //lets iterate messages which are located in chain
                for (int i = 0; i < getMessages().size(); i++){
     
                    //Convert element(i) to Message object
                    Message inChain = (Message)getMessages().element(i);
     
                    //create new LinkedList to store all replies from messages
                    LinkedList replies = new LinkedList();
                    //if searched id is matched in chain messagelist, create new message and add it to
                    if (inChain.getId() == id){
                        Message m1 = new Message(comment);
                        inChain.addReply(m1);
     
                    // if message was not found from chains linkedlist
                    //it is reply to a reply, and located in linked list linked to message
                    }else{
     
                    //add replies from message to linkedlist object
                    replies = inChain.getReply();
     
                    //iterate over replies    
                    for (int j = 0; j < replies.size(); j++){  
                        Message reply = (Message)replies.element(j);
                        //if reply id matches, add it to messages "reply" list
                        if (reply.getId() == id);
                            Message m2 = new Message(comment);
                            reply.addReply(m2);
                           }
                       }
     
                    }
     
                }
            }
         //returns linkedlist that has messages without replies
        public LinkedList getMessages(){
            return messages;
       }
     
       //adds new message to chain
      public void addNewMessage(Message m){
        messages.add(m);
        }
    }
    I didin't add the printing method, since it works with just using message class. Problem is in that method in chain class. If I add new messages to the chain and add replies to those messages maually like this, everything works: Chain.addMessage(message1), message1.addReply(new message). I need to use my program in a way that I search message with ID from either the messages reply list or message list in chain. Thx in advance and sorry for poor english
    Last edited by Norm; March 11th, 2018 at 07:04 AM. Reason: Added code tags

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java LinkedList problem

    program can print properly 2 replies, so indent of "----", but after that it doesn't work.
    What is wrong with the current output?

    Be sure to add code tags to any posted code.

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    The posted code has compiler errors. Please post code that will compile without errors so it can be executed for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java LinkedList problem

    it just just supposed to be "pseudocode" to show what I'm trying to achieve. I have 2 classes: Messagechain and Message. Messagechain has linkedlist that adds all new message entries into that. Message class has also linkedlist and all the replies to messages go to there. So in chain there are messages in two different lists. In the messagechains message list or in the list associated with messages(replies to messages). How should I search message by ID in the right manner in this case? First I'm checking if ID is present in the messagechains list, if not I check messages reply list. My function stops working after this part since it cannot find id from those messages which are replies to others. Chain<---new message<---reply-<---reply<---THIS IS NOT FOUND

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java LinkedList problem

    it just just supposed to be "pseudocode" to show
    How can anyone compile and test that code to see and find the problem?

    How should I search message by ID
    Have a loop that looks at each message and tests for ID.
    What is the method supposed to return when it finds a match?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Populating a LinkedList in java
    By pyler in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 18th, 2014, 01:14 PM
  2. [SOLVED] linkedlist concurrentmodificationexception
    By dEvilKinG in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 25th, 2013, 12:20 PM
  3. [SOLVED] Sorting LinkedList
    By wdh in forum What's Wrong With My Code?
    Replies: 28
    Last Post: April 29th, 2012, 12:52 PM
  4. Weird problem with my linkedlist
    By clydefrog in forum Collections and Generics
    Replies: 19
    Last Post: February 22nd, 2012, 06:47 PM
  5. Replies: 3
    Last Post: February 20th, 2012, 10:11 AM