Solved

for loop

Posted on 2007-11-20
17
224 Views
Last Modified: 2010-03-30
code shown below..

index[0] and index[1] have values 0 and 6 resp.

but the programs comes out of loop after checking 6 value

am missing something??
for(int m=0; m<12; m++) {
   if(m==index[0] || m==index[1]) {
	log.info("keeping "+m+" instance");
   } else {
	log.info("delete "+m);
	startingData.delete(m);
}
}

Open in new window

0
Comment
Question by:max_dub
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 4
  • +1
17 Comments
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20324269
Can you post the rest of code, please.
It seems to me that for loop must have 12 iterations.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20324275
Make sure you're not ignoring exceptions
0
 
LVL 92

Expert Comment

by:objects
ID: 20324278
whats the log show?
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:max_dub
ID: 20324309
that is pretty much the code where runtime error comes

log shown below

as per exceptions, the only thing i need to check is if the counter equals either if index value .. keep data .. otherwise delete data

but somehow it doesn't loop entirely upto 12
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - image 1 to delete: 0
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - image 2 to delete: 6
 
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - Starting data: 12
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - keeping 0 instance
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - delete 1
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - delete 2
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - delete 3
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - delete 4
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - delete 5
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - keeping 6 instance
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - Starting data: 7
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - Query Data: 10
2007-11-20 23:23:16,406 WARN  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - All Loaders Spawned

Open in new window

0
 

Author Comment

by:max_dub
ID: 20324351
ok, it seems to give arrayoutofbounds exception for no. 7

debugging now
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20324375
>>as per exceptions, the only thing i need to check is if the counter equals either if index value .. keep data .. otherwise delete data

It's doesn't really matter what you're doing. You can get an exception almost anywhere - often terminating the thread
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20324385
>>ok, it seems to give arrayoutofbounds exception for no. 7

(A RuntimeException )
0
 
LVL 92

Expert Comment

by:objects
ID: 20324404
get a stack trace f the exception, ando stop it breaking yo looppp use;

for(int m=0; m<12; m++) {
   if(m==index[0] || m==index[1]) {
        log.info("keeping "+m+" instance");
   } else {
        log.info("delete "+m);
        try {
           startingData.delete(m);
        } catch (Exception ex) {
           ex.printStackTrace();
        }
}
}
0
 

Author Comment

by:max_dub
ID: 20324460
yeah, after 6

it throws array out of bounds

java.lang.ArrayIndexOutOfBoundsException
      at java.lang.System.arraycopy(Native Method)
      at weka.core.FastVector.removeElementAt(Unknown Source)
      at weka.core.Instances.delete(Unknown Source)

wondering as the object does contains 12 instances?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20324534
You need to do some more debug to find out why
0
 

Author Comment

by:max_dub
ID: 20324541
yeah, when i am individually deleting the values it works fine

but not as a whole, i suppose its shifting the values in between

thanks for help!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20324551
No problem
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 20324571
It won't after you start removing them :)

try this:

for(int m=11; m>=0; m--) {
   if(m==index[0] || m==index[1]) {
        log.info("keeping "+m+" instance");
   } else {
        log.info("delete "+m);
        try {
           startingData.delete(m);
        } catch (Exception ex) {
           ex.printStackTrace();
        }
}
}

0
 

Author Comment

by:max_dub
ID: 20324601
wow, i was thinking of doing same, but then i thought probably it won't work.. its just the otherway round.. but its does work :)

so actually i was debugging it and each time i was delete an instance, it was shifting it upwards and when it reached 7, it was already out of that reach .. hence the array out of bounds

now i wonder, why wud from 11 to 0 work and not 0 to 11??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20324653
?
0
 

Author Comment

by:max_dub
ID: 20324724
i don't knw ??

somehow it works otherway around - from 11 to 0 .. mebbe experts can say why

thou what i was trying to do was to delete each instance individually and once it deletes an instance, i updated the count so that it deletes the right index and no exception then

but objects sol. worked too.. i still need to figure it out why?
0
 
LVL 92

Expert Comment

by:objects
ID: 20324841
as i mentioned above you won't have 12 after you start removing them.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to install java on RHEL image on EC2 4 58
ejb example issues 3 56
Which non-HTML GUI front end to use with Java? 3 52
Java 8 to Java 6 8 33
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question