1

I am trying to add multiple values to an array, but it returns a cannot resolve error. Is there any way or work-around here?

for (Map.Entry<String,String> entry : hash.entrySet()) {
          String key = entry.getKey();
          String value = entry.getValue();

          if(typePHP.equals("TARGET")) {
              message += key + " " + value + "t\n";
           }else {
              message += key + " " + value + "r\n";
           }
          array.add(Integer.parseInt(key),value,typePHP,gamePHP);
          //Heres the error.
    }

Here's the error:Cannot resolve method 'add(int, java.lang.String, java.lang.String, java.lang.String)'

I need the single element to store 4 values at once and have an output like {109,40, TYPE, GAME};

9
  • please post full error log Commented Jun 18, 2019 at 6:38
  • please check again @Fartab Commented Jun 18, 2019 at 6:39
  • The error you posted says that array does not have add method. what is the type of array? please mention its declaration. Commented Jun 18, 2019 at 6:41
  • ArrayList<String> array = new ArrayList <>(); @Fartab, I can add values to it but I can only add 1 value at a time, I need it to be 4 values at once. Commented Jun 18, 2019 at 6:44
  • you need to use 'push', like: array.push(item1, item2, ..., itemX), the add-method doen't work Commented Jun 18, 2019 at 6:44

3 Answers 3

2

First, ensure that your ArrayList is currently storing an object with a constructor of (String a, String b, String c). When you call array.add(new Item(a, b, c), Java needs to match that to an existing method.

public class Item {
public Item(String a, String b, String c) {
        // initialize your object's variables here in the constructor
    }

}

Then, you have to use new when you're creating a new object using add().

array.add(new Item(a, b, c));

See this from Java: Adding new objects to an ArrayList using a constructor

And this reference from Oracle about constructors: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

Sign up to request clarification or add additional context in comments.

Comments

1

use addAll in this way:

array.addAll(Arrays.asList( key,value,typePHP,gamePHP) );    


UPDATE: You need a 2d array, based on this sentence I need the single element to store 4 values at once and have an output like {109,40, TYPE, GAME};

So, try this:

    ArrayList<ArrayList<String>> array2d = new ArrayList<>();
    array2d.add(new ArrayList<String>() {{
        add("109");
        add("40");
        add("TYPE");
        add("GAME");
    }});

In your case, there should be something like this:

ArrayList<ArrayList<String>> array2d = new ArrayList<>();

for (Map.Entry<String,String> entry : hash.entrySet()) {
      String key = entry.getKey();
      String value = entry.getValue();

      if(typePHP.equals("TARGET")) {
          message += key + " " + value + "t\n";
       }else {
          message += key + " " + value + "r\n";
       }

      array2d.add(new ArrayList<String>() {{
        add(key);
        add(value);
        add(typePHP);
        add(gamePHP);
    }});
}

3 Comments

This returns it as a single element, I need it to only contain 4 string values at once in 1 element.
I've updated the solution to contain 4 string values at once in 1 element
Your updated answer works perfectly, thank you! I also accepted it as correct answer
0

You can create Custom object of your choice member

public class Item {

    private int key;
    private  String value;
    private String typePHP;
    private String gamePHP;

    public Item(int key, String value, String typePHP, String gamePHP) {
        this.key = key;
        this.value = value;
        this.typePHP = typePHP;
        this.gamePHP = gamePHP;
    }

    public int getKey() {
        return key;
    }

    public void setKey(int key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getTypePHP() {
        return typePHP;
    }

    public void setTypePHP(String typePHP) {
        this.typePHP = typePHP;
    }

    public String getGamePHP() {
        return gamePHP;
    }

    public void setGamePHP(String gamePHP) {
        this.gamePHP = gamePHP;
    }
}

Now you can use like

ArrayList<Item> itemArray = new ArrayList <>();

for (Map.Entry<String,String> entry : hash.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();

    if(typePHP.equals("TARGET")) {
        message += key + " " + value + "t\n";
    }else {
        message += key + " " + value + "r\n";
    }
    itemArray.add(new Item(Integer.parseInt(key),value,typePHP,gamePHP));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.