Hello everyone...! this is my first post here. I have a problem regarding finding the average from an arraylist containing database values.

From the database using getString() method i have stored the results of 3 fields in 3 separate arrylist. now what i needed is to find the average from those 3 arraylist separately means i need the average values of the 3 fields separately.

can anyone help me out of it. Thanks in advance.

Convert the Strings to numbers, add them up, divide by the number of entries.

Integer.parseInt(String a);
Use above method to convert String into integer

Thank you Sir, for your reply. I did like this but it gave me en exception that----

Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String.

Actually i am using 2 classes. One class DB where i query the result and store the values in 3 separate arraylist and return the values in a separate arraylist containing the 3 separate arraylist.

Now i declare an object of DB in another class and call the appropriate method of DB and try to do the avg calculation, but i found the above mentioned exception.

Please give me some suggestion how i can do the calculation.

Thank you.

The string that you are trying to cast into an integer must be an integer.
Eg:

String value = "2";
int intFromValue = Integer.parseInt(value);

Hope this helps.

Thank you sir for your reply.

But sir, the string that i am trying to cast is a double and i used..

double ca= Double.parseDouble(str);

Actually sir whether i have to iterate the arraylist?

Thank you.

You would need to do it for each element. So, yes you need iterate through the list and keep on casting them.

Edit: Also you would need to be adding the current value to the previous value. I hope this makes sense.

Thank you sir. Again i need i simple suggestion. Since my arraylist contains 3 separate arraylist so while iterating how do it knows that it iterate for the first arraylist values or for the second arraylist values.

Thank you for you replies. Now this is a doubt in my mind. After clearing this i could able to do the avg calculation.

Thank you sir. please give me a suggestion regarding this.

From your explanations I understood that you have 3 ArrayLists (let's say l1, l2 and l3) inside an ArrayList (al). If this is the case, one way you could do is to use an enhanced for loop to loop through al. Now, you will get l1 during the first loop, l2 and then l3. Manipulate l1, l2 and l3 like you would a normal ArrayList. Here is an example:

ArrayList<Double> l1 = new ArrayList<Double>();
ArrayList<Double> l2 = new ArrayList<Double>();
ArrayList<ArrayList> al = new ArrayList<ArrayList>();
//add elements to l1 and l2. //add l1 and l2 to al. 
 
for(ArrayList d: al){//enhanced for loop.
//d = l1 the first time. d=l2 the second time
 for(int i=0; i<d.size(); i++){ //loop through l1 and l2 
  System.out.println(d.get(i)); //d.get(i) gives you the double values stored in l1,l2
 }
}

Hope this helps and good luck.

Thank you sir for your immense help. Now, i can continue further with my work....

Thank you...!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.