How Can I Ensure Messages in Java ArrayList Are Sorted by Time Descending Order?

  • Comp Sci
  • Thread starter blah45
  • Start date
  • Tags
    Hard Java
In summary, the Message class will be used to store the details of a message. It will have the following fields: from, message, readableTime, and timeReceived. The MessageList class will be used to store a list of messages. It will have the following fields: from, message, readableTime, and timeReceived. The Message class will be immutable and support the following operations: add, move, and getNumber. The getTime() method will return the time in milliseconds since the system was started. The getMessage() and getNumber() methods will return the text and number of the current message, respectively.
  • #1
blah45
65
0
Java problem ArrayList problem, **URGENT** hard question

Introduction:

You are part of a team working on a software system for a mobile phone company. The team is working on the development of the component that will be used for storing text messages. The initial design has a structure using two classes.

THE MESSAGE CLASS:

The first class is called Message and will be used to store the details of a message.
A message contains the following:

String from the number of the person sending the message.

String message the actual text of the message

long timeReceived the time the message was received. The timeReceived value can be set from the System clock.

Once created messages are immutable.

THE MESSAGELIST CLASS:

The second class is called MessageList. It is used for storing a list of messages (i.e. instances of the Message class). A message list is initially empty. Messages can be added, removed, displayed and moved from one list to another. The contents of a list can also be erased. NOTE, the messages in a MessageList are ALWAYS stored in descending timeReceived order (i.e. most recently received is first).
The class should support/provide the following operations

• Allow new messages to be inserted into the list. Message lists are stored in descending timeReceived order (i.e. most recent first). The header for the add operation is
public void add(Message m).

• Allow messages to be moved from one list to another. When a message is moved it is removed from the original list and added to the receiving list. If the receiving list does not exist then the message should not be removed from the original list.
public void move(int position, MessageList ml)


***************************************
************** MY SOLUTION**************
***************************************
**NOTE**

There is other methods but these to are the only too I need help on. Please help Thanks =D
I don't want to optimize the code, the methods run fine as they are bar the adding messages in time descending order. please help

**/Note**
Code:
//This is the Message class//

public class Message
{
    
    private String from ;
    private String message ;
    private long timeReceived  ;
    private String readableTime ;

    public Message(String Nmbr, String msg)
    {
        from = Nmbr ;
        message = msg ;
        readableTime = getTime() ;
        timeReceived = System.currentTimeMillis() ;
        
        System.out.println("This message is from " + from + " at " + getTime()) ;
        System.out.println(message) ;

    }

    private String getTime() 
    {        
        long hour, min, sec ;
        long currentTime = System.currentTimeMillis() ;
        String formattedTime ;
        sec = currentTime / 1000 ;
        sec = sec % (24*60*60);
        min = sec / 60 ;
        sec = sec % 60 ;
        hour = min / 60 ;
        min = min % 60 ;

        if(hour < 13) {
            if(hour == 0) {
                hour = 12 ;
            }
            formattedTime = hour + ":" + min + ":" + sec + "am" ;
        }else {
            if(hour != 12) {
                hour = hour - 12;
            }
            formattedTime = hour +":"+ min + ":" + sec + "pm" ;
        }
        return formattedTime ;

    }
    
    public String getMessage()
    {
        return message ;
    }
    
    public String getNumber()
    {
        return from ;
    }
    
    public String getMessageTime()
    {
        return readableTime ;
    }
    
    public long getTimePc()
    {
        return timeReceived ;
    }
} 


//This is the MessageList class//

import java.util.* ;
public class MessageList
{
    private ArrayList<Message> MessageList ;

    public MessageList()
    {
        MessageList = new ArrayList<Message>()  ;    
    }

    public int size()
    {
        return MessageList.size() ;
    } 

    public void display()
    {
        if(MessageList.size() == 0)
        {
            System.out.println("There is no messages in the list") ;    
        }
        else 
        {
            int top = MessageList.size() - 1 ;
            int j = 1 ;
            for(int i = 0 ; i < MessageList.size() ; i++ )
            {

                Message msg = MessageList.get(top) ;
                System.out.println("Message " + j + "is ---> " +  msg.getMessage()) ;
                j++ ;
                top-- ;

            }
        }
    }

    public void Display(int message)
    {
        int top = MessageList.size() - 1 ;
        if(MessageList.size() == 0)
        {
            System.out.println("There is no messages in the list") ;    
        }

        else if(message < MessageList.size())
        {
            Message msg = MessageList.get(message) ;
            System.out.println(msg.getMessage()) ;
            top-- ;
        }

    }

    public void addMsg(Message msg)
    {
        if(MessageList.size() == 0)
        {
            MessageList.add(msg) ;
        }

        else
        {
            int i = 0 ;
            int j = 0 ;
            while(i <= MessageList.size())
            {
                int top = MessageList.size() - 1 ;
                Message msg1 = MessageList.get(i) ;                                                      
                 
                if(msg1.getTimePc() > msg.getTimePc())
                {
                    MessageList.add(msg) ; 
                }
                else //(msg.getTimePc() < msg.getTimePc())
                {
                    Message m = MessageList.get(top) ;
                    MessageList.remove(top);
                    MessageList.add(msg) ;

                }
                i++ ;
                j++ ;
            }
        }
    }  
    

    public Message getMSG(int position)
    {
        Message msg = null ;

        if( position >= MessageList.size())
        {

            System.out.println("There is no message at " + position) ;

        }

        else
        {
            msg = MessageList.get(position) ;
            System.out.println("There is a message at index " + position) ;
            System.out.println("The Message is : " + msg.getMessage()) ;                           
        } 
        return msg ;
    }

    public int find(String searchText)
    {
        int i = 0 ;
        boolean found = false ;
        int posMsg = 0 ;
        while(i < MessageList.size() && !found)
        {
            Message msg = MessageList.get(i) ;
            String MSG = msg.getMessage() ;

            if(MSG.indexOf(searchText) >= 0 )
            {                             

                posMsg = i ;
                found = true ;
            } 
            else
            {
                posMsg = -1 ;

            }

            i++ ;
        }
        return posMsg ;
    }

    public void remove( int position)
    {
        Message msg = MessageList.remove(position) ;
    }

    public void  move(int position, MessageList ml)
    {
        int i = 0;
        Message msg = MessageList.get(position) ;

        while(i < MessageList.size())
        {
            ml.addMsg(msg) ;
            remove(position) ;
            i++ ;
        }

    }

    public void erase()
    { 
        int i = 0 ;
        while( MessageList.size() != 0 )
        {
            MessageList.remove(i) ;
            i++ ;
        }

        System.out.println("The List has been erased") ;
    }

    public boolean isEmpty()
    {   
        boolean exists = false ;
        if(MessageList.size() == 0)
        {
            exists = true ;
        }

        return exists ;
    }
}
 
Physics news on Phys.org
  • #2


Can you be more specific about the help you need? Of the two methods you're having trouble with, the addMsg method should be renamed to add, according to the problem specification you provided.
 
  • #3


I'm stuck in how to add message's int time descending order, so if i make 2 messages, the second will be the most recent so that should go to the top of the MessageList and then message 1 should be below that, but the user can add them in any way they want so I don't know how to make sure the messages are being added in the correct order at the moment it adds one message and thats
 
  • #4


Here's what I would do in the add method.

If the messageList is empty, just add the new message using the add() method on MessageList (inherited from ArrayList).

If the messageList is not empty, put the message in the right place. You can do this by writing a loop of some kind that compares timeReceived on the message you want to add to the timeReceived on each message in messageList. If the first message in messageList is older than your message, insert the new message in front of the old message, using the add(index, element) method on ArrayList. If the first message in messageList is newer than the message you want to add, don't do anything. Continue on with the loop.

A new message will go into one of three places: the beginning (it's newer than any message already in messageList); somewhere in the middle; the end (it's older than any existing message in the list).

If you store your messages in order, your list remains sorted by reception time, so it's relatively easy to insert new messages in the right place. The worst case is when the message to insert is older than all the ones in the list, since you have to check each message in the list. On average, you'll need to check half of the messages in the list.

Hope that helps.
 
  • #5


Why are you using an ArrayList for this? If you want to be able to insert properly, it should be a linked list. Inserting anything in an array except for at the end is nasty, and that's what linked lists are for (it's class LinkedList).

Now if you needed to insert a message that has a time that would place it somewhere in the middle of the list, you can either binary search or linear search until you find the right place and just insert it there.

EDIT: Also the erase method only needs to call list.clear(). No need to do it all manually. In fact, the method should really be called clear() to be consistent with the rest of Java and the collections classes.
 
  • #6


The add(index, element) method on ArrayList inserts an element at the specified index, and takes care of re-indexing the elements after the insertion point in the ArrayList (according to the docs). That makes using this class a bit easier to use than trying to insert a value into an ordinary array.
 
  • #7


Mark44 said:
The add(index, element) method on ArrayList inserts an element at the specified index, and takes care of re-indexing the elements after the insertion point in the ArrayList (according to the docs). That makes using this class a bit easier to use than trying to insert a value into an ordinary array.
Indeed, I know ArrayList can do the job. I should elaborate. I mean adding would probably happen a lot more often than move, so a linked list might be more appropriate and efficient. Even if the class handles moving entries, it still needs to move them, which is expensive.

On the other hand, I suppose that it probably won't affect marks received, so can be safely ignored.

As I look at the source a little more, a few small comments:

The name of the list should not start with an upper case letter. That's reserved for classes. So it should not be:
Code:
    private ArrayList<Message> MessageList ;
But rather:
Code:
    private ArrayList<Message> messageList ;

Looking at your add code, the only thing I see wrong (after a quick glance) is that you're not inserting at a specific location in the list. Use this version of add:

void add(int index, E element)

Also not sure why you have a variable called 'j' there, though I'm guessing you have plans for it.

Another place where you didn't start a variable with a lower case letter. But worse, you have two variables with the same name but different capitalization. Pretty ugly (I'd change MSG to msgString or something like that):
Code:
            Message msg = MessageList.get(i) ;
            String MSG = msg.getMessage() ;

Also, getMSG() should probably just be called "get()" unless I missed something in your specs.
 
  • #8


Thanks for all the help so far, this is what I have now, i get an infinite loop but i think the if's and main bodys are right.

Code:
 public void addMsg(Message MsgBeingAdded)
    {        
        int i = 0 ;
        
        if(MessageList.size() == 0)
        {
            MessageList.add(MsgBeingAdded) ;
        }
        else if ( MessageList.size() != 0 )  
        {

            while(i < MessageList.size())
            {
                Message MsgInList = MessageList.get(i) ;

                if(MsgInList.getTimePc() > MsgBeingAdded.getTimePc())
                {
                    MessageList.remove(MsgInList) ;
                    MessageList.add(MsgBeingAdded) ;
                    MessageList.add(MsgInList) ;

                }

                else if(MsgInList.getTimePc() < MsgBeingAdded.getTimePc())
                {
                    MessageList.add(MsgBeingAdded) ;
                }
                i++ ;
            }
        }
    }
 
  • #9


You are using the wrong add overload. The one you are using inserts an object at the end of the ArrayList. That's not what you want. Use the add method with two arguments: the index and the object to add.

Your code should not remove a message and then add it back in, as you do here:
Code:
MessageList.remove(MsgInList) ;
MessageList.add(MsgBeingAdded) ;
MessageList.add(MsgInList) ;

Your first if-else block should be improved. In the if part you check to see if MessageList.size() is equal to 0.

In the else part you check to see if MessageList.size() is not equal to zero. The whole if-else block can be simplified like this:
Code:
if(MessageList.size() == 0)
{
     MessageList.add(MsgBeingAdded) ;
}
else   
{
  // What happens if the list is not empty.
  .
  .
  .
}
The list is either empty, which your boolean expression in the if statement checks, or it isn't. There are no other possibilities.

The MessageList property of the class of the same name should be changed to messageList.

Instead of calling messageList.size() three times, just call it once and store it in a local variable.
Code:
int listSize = messageList.size();

if (listSize == 0)
{ 
   .
   .
}
else
{
  .
  .
  .
}

Your if-else block in your while loop covers only two of the three possibilities. What happens if the timeReceived values for the two messages are the same?

Instead of a while loop, I would use a for loop.

You need to think some more about your algorithm for inserting a message in the message list. If the times of the messages in messageList are 12:02, 11:48, and 10:30, where would a message go whose timeReceived was 10:45? 11:52? 10:15?

The member names you are using aren't what they should be. If you use names other than those listed in the problem statement, your instructor will probably mark your work down.
The names listed in your first post for the Message class are from, message, and timeReceived. There is no mention in the first post of readableTime

The names of the methods in the MessageList class are add (not addMSG or addMsg) and move.
 
  • #10


The names are just down to preference that's all, I find it easier to understand the way I've done it, now here is my latest attempt, It should work but I get an out of bounds error

Code:
public void addMsg(Message MsgToAdd)
    {    
        int i = 0;
        boolean found = false ;        
        if(MessageList.size() == 0)
        {
            MessageList.add(MsgToAdd) ;
        }

        else
        {
            Message MsgInList = MessageList.get(i) ;
            while(found == false)
            {

                if( MsgToAdd.getTimePc() > MsgInList.getTimePc() )      //Is the time of the msg I'm trying to add bigger than the msg that's at the top of the list
                                                                        //if true then just add msg.     
                {                                                                     
                    MessageList.add(i,MsgToAdd) ; 
                    found = true ;
                }
                else 
                {
                    while(MsgToAdd.getTimePc() < MsgInList.getTimePc())    // Is the time of the msg smaller than what's at the top of the list, if true                                                                            //
                    {                                                      //then go to the next msg in the list and whenever the loop stops you know that "i"
                        MsgInList = MessageList.get(i) ;                   //position is where you add that msg.
                        i++ ;
                    }
                    MessageList.add(i,MsgToAdd) ;
                    found = true ;
                }

            }
        }
    }
 
  • #11


Cathal Cronin said:
The names are just down to preference that's all, I find it easier to understand the way I've done it, now here is my latest attempt, It should work but I get an out of bounds error
There are naming conventions everyone uses which, if broken, confuse and annoy programmers everywhere. And part of that is variable labels and methods start with lower case, classes start with upper case. I would stick to it. If I had to work on someone's code that broke such basic conventions, I would be cursing his/her name in no time.

Also, the reason we suggest addMsg should be called just 'add' is because that's what you told us in the specs in your first message. You said it should be "public void add(Message m)". Of course, you can call it whatever you want, but there's certain naming conventions used throughout Java that are expected by people. That's one of them. Notice in the ArrayList class and other collections classes how it's always just "get". Not "getElement", "getEntry" or anything else. Just "get". The collection classes would be much more annoying if you had to learn different naming conventions for every class.

As for the bug, it should be easily findable with a little debugging. First thing, find the exact line which fails. It is the third line in this code snippet (with the get):
Code:
                    while(MsgToAdd.getTimePc() < MsgInList.getTimePc())
                    {
                        MsgInList = MessageList.get(i) ;
                        i++ ;
                    }

Look at it closely... You know that line is the one that fails, and you know it's going past the end of the array (or past the beginning, but the end is more likely looking at the code). The exception contained all the information you needed to fix this error. Where are you making sure you didn't go past the end of the array?
 
  • #12


This should work but it's not giving me the correct order.(Or at least I think it should work lol)
Thanks again for the help.
Code:
public void add(Message MsgToAdd)
    {    
        int i = 0;
        boolean found = false ;        
        if(messageList.size() == 0)
        {
            messageList.add(MsgToAdd) ;
        }

        else
        {
            Message MsgInList = messageList.get(i) ;
            while(found == false)
            {

                if( MsgToAdd.getTimePc() > MsgInList.getTimePc() )          
                {                                                                     
                    messageList.add(0,MsgToAdd) ; 
                    found = true ;
                }
                else 
                {
                    while(MsgToAdd.getTimePc() < MsgInList.getTimePc() && i < MessageList.size())                            
                      {   
                              MsgInList = messageList.get(i) ;
                               i++ ;
                       }
                    MessageList.add(i,MsgToAdd) ;
                    found = true ;
                }

            }
        }
    }
 
Last edited:
  • #13


Cathal Cronin said:
This should work but it's not giving me the correct order.(Or at least I think it should work lol)

I always laugh when I hear a programmer say "It should work". In almost all cases, of course, no it shouldn't work because there's an error. If there wasn't, it would work. That said, I'm also sometimes guilty of saying that same phrase.

From what I can see from quick testing, it basically seems to work. There's one problem for testing here, and that's that the code executes so quickly that your timer may not even have incremented by the time you add the second message. Depends on the resolution of your timer and how often it updates. Because if they have the same time, then there's no specific order for them.

As I recall (could be wrong), you can only "sleep" in a thread. It's a hack, but for really quick testing, you can add a for loop or something like that just to waste time.

I added this main to the MessageList class just to kick the tires which might help. I wouldn't do this in a proper test, though. It's cheesy and brittle in the extreme to add for loops for delays.

Code:
    public static void main(String args[]) {
        MessageList ml = new MessageList();
        Message msg1 = new Message("1", "Message 1");
        for (int i = 0; i < 1000000; i++);
        Message msg2 = new Message("2", "Message 2");
        for (int i = 0; i < 1000000; i++);
        Message msg3 = new Message("3", "Message 3");
        for (int i = 0; i < 1000000; i++);
        Message msg4 = new Message("4", "Message 4");
        ml.add(msg4);
        ml.add(msg2);
        ml.add(msg3);
        ml.add(msg1);
        ml.display();
    }

They seem to come out in order even when I add them out of order. Print out the full time (as in getTimePc() I think it was called) instead of the formatted time for debugging purposes and make sure the times end up different. I'd guess that might be the problem.
 
  • #14


Add 5 or more messages it doesn't get them in the order then, probably for the reason you said. How do I fix the code without making "delaying it". Is it just down to the system or is there more bugs, I'm guessing the later. I ran it and it doesn't give the right order still. I made sure that the time is in milliseconds when it compares the messages
 
Last edited:
  • #15


Cathal Cronin said:
Add 5 or more messages it doesn't get them in the order then, probably for the reason you said. How do I fix the code without making "delaying it". Is it just down to the system or is there more bugs, I'm guessing the later. I ran it and it doesn't give the right order still. I made sure that the time is in milliseconds when it compares the messages
Your timer is in milliseconds, and quite a lot happens in a millisecond on a modern PC. Here's a cleaner way to delay until the timer changes:

while (msg.getTimePc() == System.currentTimeMillis());

However, that's not necessary. There's a timer available with a better resolution that you can use. Try using the System.nanoTime() method. On my system, it totally resolves the problem.
 
  • #16


Okay, Here's all my code so far, Sorry for all the naming errors etc...it all compiles and works except the add message method, it adds them but not in order, even with the nano seconds and the time delay.

Code:
//THE MESSAGE CLASS//


public class Message
{
    
    private String from ;
    private String message ;
    private long timeReceived  ;
    private String readableTime ;

    public Message(String Nmbr, String msg)
    {
        from = Nmbr ;
        message = msg ;
        readableTime = getTime() ;
        timeReceived = System.nanoTime() ;
        
        System.out.println("This message is from " + from + " at " + getTime()) ;
        System.out.println(message) ;

    }

    private String getTime() 
    {        
        long hour, min, sec ;
        long currentTime = System.currentTimeMillis() ;
        String formattedTime ;
        
        sec = currentTime / 1000 ;
        sec = sec % (24*60*60);
        min = sec / 60 ;
        sec = sec % 60 ;
        hour = min / 60 ;
        min = min % 60 ;

        if(hour < 13) {
            if(hour == 0) {
                hour = 12 ;
            }
            formattedTime = hour + ":" + min + ":" + sec + "am" ;
        }else {
            if(hour != 12) {
                hour = hour - 12;
            }
            formattedTime = hour +":"+ min + ":" + sec + "pm" ;
        }
        return formattedTime ;

    }
    
    public String getMessage()
    {
        return message ;
    }
    
    public String getNumber()
    {
        return from ;
    }
    
    public String getMessageTime()
    {
        return readableTime ;
    }
    
    public long getTimePc()
    {
        return timeReceived ;
    }

} 
    //THE MESSAGE LIST CLASS//

import java.util.* ;
public class MessageList
{
    private ArrayList<Message> MessageList ;

    public MessageList()
    {
        MessageList = new ArrayList<Message>()  ;    
    }

    public int size()
    {
        return MessageList.size() ;
    } 

    public void display()
    {
        if(MessageList.size() == 0)
        {
            System.out.println("There is no messages in the list") ;    
        }
        else 
        {
            int top = MessageList.size() - 1 ;
            int j = 1 ;
            for(int i = 0 ; i < MessageList.size() ; i++ )
            {

                Message msg = MessageList.get(top) ;
                System.out.println("Message " + j + " is ---> " +  msg.getMessage()) ;
                j++ ;
                top-- ;

            }
        }
    }

    public void Display(int message)
    {
        int top = MessageList.size() - 1 ;
        if(MessageList.size() == 0)
        {
            System.out.println("There is no messages in the list") ;    
        }

        else if(message < MessageList.size())
        {
            Message msg = MessageList.get(message) ;
            System.out.println(msg.getMessage()) ;
            top-- ;
        }

    }

    public void addMsg(Message MsgToAdd)
    {    
        int i = 0;
        boolean found = false ;        
        if(MessageList.size() == 0)
        {
            MessageList.add(MsgToAdd) ;
        }

        else
        {
            Message MsgInList = MessageList.get(i) ;
            while(found == false)
            {
                if(MsgToAdd.getTimePc() > MsgInList.getTimePc())   
                {
                    MessageList.add(0,MsgToAdd) ; 
                    found = true ;
                }                                                                                          

                else 
                {
                    i = 0 ;
                    while(MsgToAdd.getTimePc() < MsgInList.getTimePc() && i < MessageList.size())                                                                 //
                    {   
                        MsgInList = MessageList.get(i) ;
                        i++ ;
                    }   
                    MessageList.add(i,MsgToAdd) ;
                    found = true ;
                }                                               
            }
        }
    }

    public Message getMSG(int position)
    {
        Message msg = null ;

        if( position >= MessageList.size())
        {

            System.out.println("There is no message at " + position) ;

        }

        else
        {
            msg = MessageList.get(position) ;
            System.out.println("There is a message at index " + position) ;
            System.out.println("The Message is : " + msg.getMessage()) ;                           
        } 
        return msg ;
    }

    public int find(String searchText)
    {
        int i = 0 ;
        boolean found = false ;
        int posMsg = 0 ;
        while(i < MessageList.size() && !found)
        {
            Message msg = MessageList.get(i) ;
            String MSG = msg.getMessage() ;

            if(MSG.indexOf(searchText) >= 0 )
            {                             

                posMsg = i ;
                found = true ;
            } 
            else
            {
                posMsg = -1 ;

            }

            i++ ;
        }
        return posMsg ;
    }

    public void remove( int position)
    {
        Message msg = MessageList.remove(position) ;
    }

    public void  move(int position, MessageList ml)
    {
        int i = 0;
        Message msg = MessageList.get(position) ;

        while(i < MessageList.size())
        {
            ml.addMsg(msg) ;
            remove(position) ;
            i++ ;
        }
    }

    public void erase()
    { 
        MessageList.clear() ;

        System.out.println("The List has been erased") ;
    }

    public boolean isEmpty()
    {   
        boolean exists = false ;
        if(MessageList.size() == 0)
        {
            exists = true ;
        }

        return exists ;
    }

   }

//THE MESSAGE DRIVER//


public class MessageDriver
{
    public static void main(String args[]) 
    {
        MessageList ml = new MessageList();
        int i ;

        Message msg1 = new Message("Cathal", "Hey");

        Message msg2 = new Message("James", "Hey back");       

        Message msg3 = new Message("Cathal", "Wuu2?");      

        Message msg4 = new Message("James", "can't get my the code");      

        Message msg5 = new Message("Cathal", "Mine works =D") ;      

        Message msg6 = new Message("James", "oh can i have it?") ;       

        Message msg7 = new Message("Cathal", "sure") ;
        
        Message msg8 = new Message("James", "sound") ;
        
        Message msg9 = new Message("Cathal", "bye") ;        

        Message msg10 = new Message("James", "bye back") ;
        
        ml.addMsg(msg5);
        ml.addMsg(msg1);
        ml.addMsg(msg7);
        ml.addMsg(msg3);
        ml.addMsg(msg10);        
        ml.addMsg(msg8);
        ml.addMsg(msg6);
        ml.addMsg(msg4);
        ml.addMsg(msg9);
        ml.addMsg(msg2);

        ml.display();
    }
}

This is the output I'm getting...obliviously it wrong...:(
This message is from Cathal at 11:28:19am
Hey
This message is from James at 11:28:19am
Hey back
This message is from Cathal at 11:28:19am
Wuu2?
This message is from James at 11:28:19am
can't get my the code
This message is from Cathal at 11:28:19am
Mine works =D
This message is from James at 11:28:19am
oh can i have it?
This message is from Cathal at 11:28:19am
sure
This message is from James at 11:28:19am
sound
This message is from Cathal at 11:28:19am
bye
This message is from James at 11:28:19am
bye back
Message 1 is ---> Wuu2?
Message 2 is ---> can't get my the code
Message 3 is ---> Hey back
Message 4 is ---> Hey
Message 5 is ---> oh can i have it?
Message 6 is ---> Mine works =D
Message 7 is ---> sound
Message 8 is ---> bye
Message 9 is ---> sure
Message 10 is ---> bye back
This message is from 123456 at 11:30:42am
msg 1
This message is from 123456 at 11:30:52am
msg 2
This message is from 123456 at 11:30:59am
msg 3
This message is from 123456 at 11:31:9am
msg 4
This message is from 123456 at 11:31:17am
msg 5
There is no messages in the list
Message 1 is ---> msg 2
Message 2 is ---> msg 1
Message 3 is ---> msg 4
Message 4 is ---> msg 3
Message 5 is ---> msg 5
 
  • #17


I don't think your add() method is working the way it should. Here's what I would do to insert a new message in the right place: compare the time on MsgToAdd with the times on each successive pair of messages in the list.

The comparison looks something like this:
Code:
if ( (MsgToAdd.timeReceived < MessageList.get(i).timeReceived) &&  (MsgToAdd.timeReceived > MessageList.get(i + 1).timeReceived) )
{
   // Insert MsgToAdd between the two messages.
}

You could put the if statement above inside a for loop, and break out of the loop (with break;) when the if condition is met. The loop should run for i between 0 and n - 2, where n is the number of messages in the message list.

You'll need to work out the special cases of when there are no messages in the list and when there is only one message in the list.
 
  • #18


So I would just insert it at the i'th position and then every other message would get shifted down the list,Yeah, and then take of the exceptions
 
  • #19


Almost - The message at index i would stay where it is, the new message would go at index i + 1, and the message that was at index i + 1 would be moved to index i + 2.

Before you turn your assignment in, you should go back to see the names of methods and properties that are listed in your first post. Your instructor is very likely to deduct points for not using the names that were given. If he has test code that is supposed to interact with yours, and you aren't using the names that were given, your code won't even compile.
 
  • #20


I'm getting an infinite loop for some reason...
Code:
public void addMsg(Message MsgToAdd)
    {    
        int i = 0;
        boolean found = false ;  
        Message MsgInList ;
        if(MessageList.size() == 0)
        {
            MessageList.add(MsgToAdd) ;
        }

        else if(MessageList.size() == 1)
        {
             MsgInList = MessageList.get(i) ;
            if(MsgToAdd.getTimePc() > MsgInList.getTimePc())
            {
                MessageList.add(0,MsgToAdd) ;
            }
            else
            {
                MessageList.add(1,MsgToAdd) ;
            }
        }
        else
        {
             MsgInList = MessageList.get(i) ;             
            while(found == false)
            {
                if(MsgToAdd.getTimePc() > MsgInList.getTimePc())
                {                                                                                                                                            
                    MessageList.add(0,MsgToAdd) ; 
                    found = true ;  
                }                                  
                else 

                {
                    if((MsgToAdd.getTimePc() < MsgInList.getTimePc()) && MsgToAdd.getTimePc() > MessageList.get(i + 1).getTimePc())  
                    {                       
                        MessageList.add(i+1,MsgToAdd) ;
                        found = true ;
                    }
                   
                }
            }
        }
    }
 
  • #21


Code:
else
{
   MsgInList = MessageList.get(i) ;             
   while(found == false)
   {
       if(MsgToAdd.getTimePc() > MsgInList.getTimePc())  
       { 
          MessageList.add(0,MsgToAdd) ; 
          found = true ;  
        }                                  
       else 
       {
            if((MsgToAdd.getTimePc() < MsgInList.getTimePc()) && MsgToAdd.getTimePc() > MessageList.get(i + 1).getTimePc())  
           {                       
                  MessageList.add(i+1,MsgToAdd) ;
                  found = true ;
            }
       }
    }
}
I think that the main problem is that you get MsgInList outside your while loop and don't update it inside the loop. This means that you aren't comparing the message to insert with message(i) and message(i + 1).
 
  • #22


So your saying that I'm not looping through the messages in the final else statement, yeah?
 
  • #23


You're not looping through the right ones. Suppose that i == 5 at the start of the outer else clause, so you get message[5]. The while loop is going to be looking at message[5] and message[6], message[5] and message[7], message[5] and message [8], and so on.

It should be looking at message[5] and message[6], then message[6] and message[7], message[7] and message[8], and so on, until it finds the right place.
 
  • #24


Also, I would simplify that outer else clause by taking care of all exceptional cases first. All I would put in that else clause is a loop (I would probably go with a for loop instead of a while loop). Inside the loop I would have my if statement that checked to see if MsgToAdd was between message and message[i+1].

Your outer else is more complicated than I believe it needs to be. Inside that else clause you have
while loop
.. if
.. else
...if
 
  • #25


I'm not exactually sure how to do what your saying..but this is what I wrote and I'm getting an out of bounds error, I know why but

Code:
else
{
 while(found == false)
            {
                MsgInList = MessageList.get(i) ;
                if((MsgToAdd.getTimePc() < MsgInList.getTimePc()) && MsgToAdd.getTimePc() > MessageList.get(i + 1).getTimePc())
                {
                    MessageList.add(i+1,MsgToAdd) ;
                    found = false ;

                } 
                else
                {
                    MsgInList = MessageList.get(i) ;
                    i++ ;
                }
        }
}
 
  • #26


Also how would i start at 5 if everytime you start this method it is reset back to 0?
 
  • #27


Cathal Cronin said:
I'm not exactually sure how to do what your saying..but this is what I wrote and I'm getting an out of bounds error, I know why but

Code:
else
{
 while(found == false)
            {
                MsgInList = MessageList.get(i) ;
                if((MsgToAdd.getTimePc() < MsgInList.getTimePc()) && MsgToAdd.getTimePc() > MessageList.get(i + 1).getTimePc())
                {
                    MessageList.add(i+1,MsgToAdd) ;
                    found = false ;

                } 
                else
                {
                    MsgInList = MessageList.get(i) ;
                    i++ ;
                }
        }
}

Your if should look like this, like I said several posts back.
Code:
if ( (MsgToAdd.timeReceived < MessageList.get(i).timeReceived) &&
     (MsgToAdd.timeReceived > MessageList.get(i + 1).timeReceived) )
{
   // Insert MsgToAdd at index i + 1.
}

Again, I would use a for loop instead of a while loop. A for loop is preferable when you know how many times it will run (you know how many messages are in the message list). A while loop is preferable when it is not known how many times the loop will run.
 
  • #28


Cathal Cronin said:
Also how would i start at 5 if everytime you start this method it is reset back to 0?
I don't understand what you're asking here.
 
  • #29


I did this a few post back but then you said I was comparing them incorectly, this is what you said to do based on your last post but It only print's out 2 out of 5 messages from my driver class

Code:
public void addMsg(Message MsgToAdd)
    {    
        int i = 0 ;
        boolean found = false ;  
        Message MsgInList ;
        if(MessageList.size() == 0)
        {
            MessageList.add(MsgToAdd) ;
        }

        else if(MessageList.size() == 1)
        {
            MsgInList = MessageList.get(i) ;
            if(MsgToAdd.getTimePc() > MsgInList.getTimePc())
            {
                MessageList.add(0,MsgToAdd) ;
            }
            else
            {
                MessageList.add(1,MsgToAdd) ;
            }
        }
        else
        {  

            for(i = 0 ; i < MessageList.size() ; i++ )
            {
                MsgInList = MessageList.get(i) ;
                if((MsgToAdd.getTimePc() < MsgInList.getTimePc()) && MsgToAdd.getTimePc() > MessageList.get(i + 1).getTimePc())
                {
                    MessageList.add(i+1,MsgToAdd) ;                   
                } 
            }
        }
    }
 
Last edited:
  • #30


Could you just explain it again...and where I'm going wrong
 
  • #31


The code in post #29 doesn't print anything, so it's possible there's a bug in your routine that prints the contents of the message list.

Your for loop needs some adjustment.
1) Assuming there are n messages in the message list, i ranges from 0 to n - 1. When i == n - 1, you are check message[n-1] and message[n], which is past the end of your list.
2) If you find the right place to add the message, insert the message and then bail out of your loop. I mentioned this back in post #17.
mark44 said:
You could put the if statement above inside a for loop, and break out of the loop (with break;) when the if condition is met.
 
  • #32


What do you do if the if condition is not met? All possible scenarios are checked

It's probably very oblivious to you what to do but I've been at this one method for quite some time that I'm a bit distrought with it anyway..

Code:
 public void addMsg(Message MsgToAdd)
    {    
        int i = 0 ;
        boolean found = false ;  
        Message MsgInList ;
        if(MessageList.size() == 0)
        {
            MessageList.add(MsgToAdd) ;
        }

        else if(MessageList.size() == 1)
        {
            MsgInList = MessageList.get(i) ;
            if(MsgToAdd.getTimePc() > MsgInList.getTimePc())
            {
                MessageList.add(0,MsgToAdd) ;
            }
            else
            {
                MessageList.add(1,MsgToAdd) ;
            }
        }
        else
        {  

            for(i = 0 ; i == MessageList.size() - 1 ; i++)
            {
                MsgInList = MessageList.get(i) ;
                if((MsgToAdd.getTimePc() < MsgInList.getTimePc()) && MsgToAdd.getTimePc() > MessageList.get(i + 1).getTimePc())
                {                   
                    MessageList.add(i+1,MsgToAdd) ;                   
                } 
            }
        }
    }
 
  • #33


It's still diplaying only two messages..out of 5
 
  • #34


You have an error here:
for(i = 0 ; i == MessageList.size() - 1 ; i++)

That should be
for(i = 0 ; i < MessageList.size() - 1 ; i++)
 
  • #35


Cathal Cronin said:
It's still diplaying only two messages..out of 5
What's your routine to print the contents of a message list?
 

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
991
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top