0
public  void dataAnalytics()
{
  double sum=0;  

    System.out.println("dataAnalytics for the Rural region");

    for (Record ee :RList)
    {
       sum=sum+RList.get(4);
    }
}

I have a very simple problem here , i am a novice java programmer , so please do be easy with me. I have an arrayLisl "Rlist" which has income in 4th index. What i am trying to accomplish is to get the sum of all income elements in the arraylist. I have tried sum=sum+RList.get(4); but i does not seem to work . income is initialized as double inthe super class but is passed in the arraylist constructor as string.

4
  • How is your ArrayList defined? Commented Feb 25, 2012 at 17:41
  • 1
    First of all, the 4th index? What do you mean? The first index for Lists is 0, so your 4th index may be 3, not 4 (perhaps you already know this). Second, you're not using the Record variable ee at all. Why do you bother in making a for loop? Third, I don't understand your explanation on how "income" is initialized. Can you add some more code on that part? Commented Feb 25, 2012 at 17:42
  • the reason for the loop is to go through each record inthe RList and retrieve the income element , and subsequently get a sum of all the income elements in the list. Commented Feb 25, 2012 at 17:47
  • for (Record ee :RList) sum=sum+Double.valueOf(ee.getIncome()); Commented Feb 25, 2012 at 17:48

2 Answers 2

1

What is the use of ee in the loop? It is unused there. Assuming you have an arraylist of Record objects and you are able to access their income fields via a getter method like getIncome() which returns a string, you should do that:

for(Record ee :RList)
    sum = sum + Double.valueOf(ee.getIncome());
Sign up to request clarification or add additional context in comments.

1 Comment

sure , im just waiting for the timer to reset :)
1

If you have stored income as a String in your ArrayList, then you must convert the String to a double type before attempting to add it to another double.

See Double.parseDouble http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Double.html

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.