• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

sum of non numeric elements in an ArrayList

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
author
Posts: 23960
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Henry
 
Vijay Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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);
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler is telling you that you can't add objects of different types.

"Dog" + 345678 causes an error.

Hunter
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're trying to add together a double and an Object, which you can't do in Java.
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
The moustache of a titan! The ad of a flea:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic