# Exception in list

krishna kumar.s
Greenhorn
Posts: 22
I am trying to solve problem 23 of project euler "http://projecteuler.net/problem=23" . The problem is i have to find sum all numbers that could not be written as sum of two abundant numbers.

The logic i used was ,1) find all the abundant numbers below 28123(this is the upper limit as mentioned in problem) store them in array add num+next and find whether it is equal to value any of value of list containing 1-28123. if eqauls remove the number . but the below programme throws an exception.

can anybody help?, is the logic correct?
at Exercise23.main(Exercise23.java:44)

Winston Gutkowski
Bartender
Posts: 10527
64
krishna kumar.s wrote:can anybody help?, is the logic correct?
at Exercise23.main(Exercise23.java:44)

Well first thing is to use the code tags correctly. The 'slash-code' tag should go at the end of the code. I'd suggest editing your post.

while(iterator.hasNext()){
looks like the culprit to me. You've clearly set 'iterator' to null, but I can't see anywhere where you set it to anything else.

As for the logic, it looks a bit tortured to me. Firstly:
for (int i=1;i<=num/2;i++) ...
is likely to make your program run far longer than it needs to.

I'd also break it up a bit more, into nicely named methods. You'll probably find it far easier to read yourself (and we certainly will).

Also, the standard loop for processing an array arr is:
for(int i = 0; i < arr.length; i++) { ...

HIH

Winston

krishna kumar.s
Greenhorn
Posts: 22
Thanks a lot, The problem is very simple i used one method to get adundant numbers . Now my modified code is throwing Exception in thread "main" .I am sorry that giving difficult in reading , i am new to programme and learning myself , first i am trying with solving project euler (i have completed 25 problems and moved to level 1)

Winston Gutkowski
Bartender
Posts: 10527
64
• 1
krishna kumar.s wrote:I am sorry that giving difficult in reading , i am new to programme and learning myself...

Winston

krishna kumar.s
Greenhorn
Posts: 22
Thanks for all the help, The issue is resolved , i used code button and tag " "at the begging as well as end

Campbell Ritchie
Sheriff
Posts: 50171
79
Why are you using a LinkedList rather than an ArrayList?
You should have your Iterator come into scope just before your loop and go out of scope just after your loop. If there is an Iterator in scope when you add to a List, you ought to get a ConcurrentModificationException.