I need to know how to set values in an associative array and display on screen

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

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
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

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


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.