Results 1 to 1 of 1
  1. #1
    Jhaz is offline Member
    Join Date
    Aug 2014
    Posts
    1
    Rep Power
    0

    Default Don't know how to explain this

    Hello. I've made a program that reads a chat log on a website, and would like to fetch only the latest 3 messages that have NOT been read by the program yet. What I have at the moment:
    Java Code:
    String[] lines = data.split(System.getProperty("line.separator"));
    Set<String> current = new HashSet<>();
    
    for (int i = 3; i < 6; i++) {
        if (lines.length < i)
            return current;
    
        if (current.contains(lines[i]))
            continue;
    
        current.add(lines[i]);
    }
    Here's the 3 latest lines. Now before I add the line to the 'current' HashSet, I would like to somehow check that the line was not in the previous 3 lines. Basically I don't want to see duplicated lines.

    This method is called whether there are new lines or not, so simply creating a List/Set of the 3 previous messages will not work.

    How would I go about doing this?

    Edit: came up with a rather simple way.
    Java Code:
    String[] lines = data.split(System.getProperty("line.separator"));
    Set<String> current = new HashSet <>();
    
    for (int i = 3; i < 6; i++) {
        if (lines.length < i)
            return current;
    
        String line = lines[i];
    
        if (current.contains(line))
            continue;
    
        if (latestMessages.contains(lines[i]))
            continue;
    
        current.add(line);
    }
    
    latestMessages.addAll(current);
    
    if (latestMessages.size() >= 7) {
        for (int i = 0; i < latestMessages.size() - 3; i++) {
            latestMessages.remove(i);
        }
    }
    Last edited by Jhaz; 08-16-2014 at 01:26 PM.

Similar Threads

  1. Replies: 0
    Last Post: 04-05-2008, 12:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •