4

Okay, so, I am working on porting an IM client over from autoit to java, and I'm still rather new to java.

What I am attempting to do is create a 2d array where I can store every user's conversation history in the array next to their name.

Then what I want to do, is, on demand, write this chat history into an edit control, by providing the user's username, thus showing the chat history with that user on screen when asked.

Tried several variations of code.

Array functions:

public class conversations {
    public static String[][] conversationspool = new String[100][];

    public static String[][] init() {
    for (int i = 0; i < 99; i++) {
        conversations.conversationspool[i] = new String[2];
    }
    return conversations.conversationspool;
    }

    public static String startConvo(String user) {
        for (int start = 0; start < 99; start++) {
            if (conversations.conversationspool[start][0] == user) {
                return conversations.conversationspool[start][1];
            }
        }
        for (int start = 0; start < 99; start++) {
            if (conversations.conversationspool[start][0] == "") {
                conversations.conversationspool[start][0] = user;
                conversations.conversationspool[start][1] = "<center>Conversation with " + user + "</center>";
                System.out.print(conversations.conversationspool[start][1]);
                return conversations.conversationspool[start][1];
                }
            }
    return "0";
    }

    public static String addToConvo(String user, String html) {
        for (int start = 0; start < 99; start++) {
            if (conversations.conversationspool[start][0] == user) {
                conversations.conversationspool[start][1] = conversations.conversationspool[start][1] + html;
                System.out.print(conversations.conversationspool[start][1]);
                return conversations.conversationspool[start][1];
            }
        }
        return "0";
    }

    public static String getConvo(String user) {
        for (int start = 0; start < 99; start++) {
            if (conversations.conversationspool[start][0] == user) {
                return conversations.conversationspool[start][1];
            }
        }
        return "0";
    }
}

When I run

                     conversations.init();
                     conversations.startConvo("ECHO");
                     SIMMain.jEditorPane.setText(conversations.getConvo("ECHO"));

I should have displayed the convo start message in my editor pane, however no matter what I do, I get the default return value of my array functions (0) I expect the returned data to be the content of the chat history for the provided user.

1 Answers1

1

Upon reaching return, the method stops executing.

//...
//array == {"no","no","yes","no","","yes",...}

for (int i = 0; i < 99; i++){
    if (array[i].equals("yes")){
      // do something
      return something;
    }
}
// this will stop at i == 2 and will not reach the end of for loop

This would explain why you would get only first encounter from the array. I assume you want all the pieces of the conversation.

I assume the init() method adds data to the variable. In the code you provided, it doesn't add anything to the array so it is empty. This is why the conditions are never met and string "0" is returned. Also, the function of init() could have been done in the first line:

public static String[][] conversationspool = new String[100][2];

== not the same as .equals() Comparing strings in java

Dmitriy
  • 91
  • 4