Solved

for loop

Posted on 2007-11-20
17
220 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
  • 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
json example 39 113
ArrayIndexOutOfBoundException 9 35
maven project error 5 21
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now