Prabu
Prabu

Reputation: 3728

Java - ArrayList item converted into each string and passing to another method

this is my method it has two argument with arrayList

private static PriorityInfo getPrioritiesInfo(ArrayList priorityNumber ,ArrayList priorityDate) {
        PriorityInfo priorityInfo = new PriorityInfo();
        ArrayList temp = new ArrayList();
        for(int i=0;i<priorityNumber.size();i++){          
            priorityInfo.setPriorityNo(toString(priorityNumber.get(i)));            
        }       

        //priorityInfo.setPriorityDate(priorityDate);
        return priorityInfo;
    }

My expected:

in the priorityNumber arraylist have n number of item (i.e 786975,293AS45), and i want to pass the each item into setPriorityNo method

the above method produce an error

method toString in class Object cannot be applied to given types;
  required: no arguments
  found: Object
  reason: actual and formal argument lists differ in length

setPriorityNo method its from PriorityInfo.class:

 public void setPriorityNo(String priorityNo) {
        this.priorityNo = priorityNo;
    }

let me know my mistake

Upvotes: 0

Views: 47

Answers (2)

Prabu
Prabu

Reputation: 3728

i'm using Apache Commons library with StringUtils

 private static PriorityInfo getPrioritiesInfo(ArrayList priorityNumber ,ArrayList priorityDate) {
        PriorityInfo priorityInfo = new PriorityInfo();
        String temp="";

        for (int i = 0; i < priorityNumber.size(); i++) {
            String value = StringUtils.join(priorityNumber.get(i), '|');
            temp = temp.concat(value);
        }
 priorityInfo.setPriorityNo(temp);       
        return priorityInfo;
    }

Upvotes: 0

Aku Nour Shirazi
Aku Nour Shirazi

Reputation: 436

You are not converting to string correctly. you are referring to the toString method in the Object class.

try something like this:

private static PriorityInfo getPrioritiesInfo(ArrayList priorityNumber, ArrayList priorityDate) {
    PriorityInfo priorityInfo = new PriorityInfo(new ArrayList());
    ArrayList temp = new ArrayList();
    for(int i=0;i<priorityNumber.size();i++){
        priorityInfo.setPriorityNumbers(priorityNumber.get(i).toString());
    }

    //priorityInfo.setPriorityDate(priorityDate);
    return priorityInfo;
}

call the .toString() method on the ArrayList priorityNumber you are passing.

and your PriorityInfo class:

class PriorityInfo {

    private ArrayList priorityNumbers;

    public PriorityInfo(ArrayList priorityNumbers) {
        this.priorityNumbers = priorityNumbers;
    }

    public void setPriorityNumbers(String priorityNo) {
        this.priorityNumbers.add(priorityNo);
    }

    public ArrayList getPriorityNumbers() {
        return this.priorityNumbers;
    }

}

dont know which other methods you have in your PriorityInfo class.

Upvotes: 2

Related Questions