-2

What I'm trying to do is get the sum of all integer items retrieved from the database then added into a LinkedList, like so:

ManageFeeNotePOJO selectedRecord = (ManageFeeNotePOJO) manageFeeNoteListTableView().getItems().get(selectdIndex);
List toFeeBillListTot = new LinkedList();
toFeeBillListTot.add(selectedRecord.getFeeNoteValue());

int sum = 0;
for (Object sumItem : toFeeBillListTot) {
    sum += sumItem;
}
System.out.println(sum);

With this I get the error:

bad operand types for binary operator '+'
first type:  int
second type: Object

The other approach is:

ManageFeeNotePOJO selectedRecord = (ManageFeeNotePOJO) manageFeeNoteListTableView().getItems().get(selectdIndex);
List toFeeBillListTot = new LinkedList();
toFeeBillListTot.add(selectedRecord.getFeeNoteValue());

int sum = 0;
for (int i = 0; i < toFeeBillListTot.size(); i++) {
    sum += (int) toFeeBillListTot.get(i);
}
System.out.println(sum);

I get java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer error with this

Please, I would be more than greatful for any working approach around this. Thank you in advance.

ILikeProgramming
  • 293
  • 3
  • 8
  • 23
  • 1
    Use a generic collection or cast each entry (or parse if you add strings). – Jeroen Vannevel Jun 08 '14 at 01:29
  • what is wrong with the other approach? – Rod_Algonquin Jun 08 '14 at 01:29
  • I've tried all the above. With `Integer.parseInt((String) toFeeBillListTot.get(i));` I get `java.lang.NumberFormatException: For input string: "1,200"`. @nachokk : `List toFeeBillListTot = new LinkedList();` does the LinkedList as shown in the question. And I've listed both errors. – ILikeProgramming Jun 08 '14 at 01:39
  • 1
    part of your problem is that I don't believe java can parse integers formatted with commas. For 1,200, you would need to remove the comma before using Integer.parseInt – Bryan Jun 08 '14 at 01:43
  • i can't understand about this line : Object sumItem : toFeeBillListTot – john Jun 08 '14 at 01:53
  • You need to understand the performance characteristics of linked lists. Your last loop runs in O(n^2) because of how you're accessing the list. – David Ehrmann Jun 08 '14 at 02:01

2 Answers2

1

First, I feel a bit weird about the use of raw types with your List declaration. By that, I mean you do not provide the type argument to it.

If you've got an expectation that you're getting back a list of Strings, then you should declare it as such:

List<String> toFeeBillListTot = new LinkedList<>();

Next, you can't simply add up Strings, as that has no meaning. You have to convert them all to some numerical type, probably Double:

double sum = 0;
for(String bill : toFeeBillListTot) {
    sum += Double.parseDouble(bill);
}

Not that the above conversion will only work if there aren't any other unusual characters in your conversion (that is, no thousands separators).

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Could you just throw in a `bill.replace(",","")` to ensure that you won't run into an error when thousands separators are present? – Bryan Jun 08 '14 at 01:45
  • (Also, if we're dealing with money, there's also the whole debate about using `BigDecimal` instead of `Double` when dealing with currency) – Dennis Meng Jun 08 '14 at 01:46
  • @Bryan There's also this question regarding the commas: http://stackoverflow.com/questions/11973383/how-to-parse-number-string-containing-commas-into-an-integer-in-java – Dennis Meng Jun 08 '14 at 01:47
  • 1
    I intentionally left this answer in this manner as to provide the main reason why the code wouldn't *compile*. There are other logic hurdles that do have to be overcome, most notably the issue with the thousands separators, but I would prefer to leave that as an exercise to the reader. My disclaimer below the actual answer should show that, at least, there is more work to be done than merely highlighting the solution, copying it, and pasting it into an IDE/text editor. – Makoto Jun 08 '14 at 01:49
1
bad operand types for binary operator '+'
first type:  int
second type: Object

This is saying: "You can't use the + operator with one these two types" - and I bet you can guess which one - Object - which, if you think about it, makes sense:

Remember that every class of every type you'll ever work with in Java extends Object. So, when you're working with something that has been cast to Object, Java knows almost nothing about what that something is. In particular, it would have no freaking clue what to do with a + applied to one.

For instance, imagine that you had a Person class and did

 Person a = new Person();
 Person b = new Person();
 a + b; //WTF?? 

Would that make sense? Of course not.

Now, you might say "but I didn't add Objects to my list, I added Integers/Doubles/some other type that the + operator should work with" - and I'm sure you would be right, but the problem is that you added them to a List of Objects, which you created as such right here:

List sum toFeeBillListTot = new LinkedList();

When you do this, you're creating an "untyped list". This means you can add objects of any type you like to it - which might sound nice, but as a natural consequence, you can only retrieve Objects from it, which is what you're doing now.

By "use a generic collection", @Jeroen is suggesting you do this instead:

List<Integer> toFeeBillListTot = new LinkedList<>();

Rather than creating a list of Objects, the <Integer> means you're creating a list of integers, which means you can only add Integers to it, and also that every element you pull out of it will be an Integer, so you can do:

int sum = 0;
for (Integer sumItem : toFeeBillListTot) {
  sum += sumItem;
}
drew moore
  • 31,565
  • 17
  • 75
  • 112