aspose file tools*
The moose likes Beginning Java and the fly likes sum of non numeric elements in an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sum of non numeric elements in an ArrayList" Watch "sum of non numeric elements in an ArrayList" New topic
Author

sum of non numeric elements in an ArrayList

Vijay Tyagi
Ranch Hand

Joined: Feb 15, 2010
Posts: 52
why is it that the sum of all elements in the ArrayList list (code below) is 10 ?
there are non numeric elements in the arraylist

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40


You are taking a "sum" of the count from zero to the size of the list.... ie 0 + 1 + 2 + 3 + 4 ==> 10.


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Vijay Tyagi
Ranch Hand

Joined: Feb 15, 2010
Posts: 52

when I change it to

sum=sum+list.get(i);

I get error

TestList.java:22: operator + cannot be applied to double,java.lang.Object
sum=sum+list.get(i);
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

The compiler is telling you that you can't add objects of different types.

"Dog" + 345678 causes an error.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
You're trying to add together a double and an Object, which you can't do in Java.


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Hunter McMillen wrote:"Dog" + 345678 causes an error.

Actually that would work, if the right references are used. Remember that + is used for string concatenations. However, in this case all references are Object references, and + definitely does not work for objects.

Vijay, print out the classes of the elements. Then find out if the numeric values have a common super class (they do, and it's not Object). If so, find a method that returns something you can add together (hint: it ends with "Value"). You can use the instanceof operator and casting to give you an instance of that common super class.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Rob Prime wrote:
Hunter McMillen wrote:"Dog" + 345678 causes an error.

Actually that would work, if the right references are used. Remember that + is used for string concatenations. However, in this case all references are Object references, and + definitely does not work for objects.


Sorry I should have said: in this case, this will cause an error.

Hunter
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 175
    
    1

Use Integer (package java.lang) class to parse int from a String[Integer.parseInt(...)].

This will return an exception if the argument cannot be converted into int data type.


Ramesh-X
samuel lenk
Greenhorn

Joined: Jun 25, 2013
Posts: 3

Hi.

If you only want to allow integers in your array list then with generics you can create your list this way. That brings the advantage that java will only let integers by added to the list and you can be sure sum will be calculated correctly. See the example:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sum of non numeric elements in an ArrayList