[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

for loop

Posted on 2007-11-20
17
Medium Priority
?
230 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 800 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

656 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