473,504 Members | 13,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem in loop

madhoriya22
252 Contributor
I am putting this code segment here.......Problem is, This loop is running only for i = 0, I dont know what is the problem.....Thanks in advance...

Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i = 0; i < v.size(); i++) {
  3.             while(itr.hasNext()) {
  4.                 Map.Entry me = (Map.Entry)itr.next();
  5.                 System.out.println("i::"+i);
  6.                 if((((WorkPackageDetailsVO)v.get(i)).getWorkPackageName()).equals(((WorkPackageSummaryVO)me.getKey()).getWorkPackageName())) {
  7.                     //System.out.println("i:"+i+" TRUE "+me.getKey());
  8.                     String status = ((WorkPackageSummaryVO)me.getKey()).getCurrentDefectStatus();
  9.                     String name = ((WorkPackageSummaryVO)me.getKey()).getWorkPackageName();
  10.                     //System.out.println("----->>>"+status);
  11.                     if(status.equals("On Work Plate"))
  12.                         System.out.println("WorkPackageName: "+name+"  Current Defect Status: "+status+"  Count: "+((WorkPackageSummaryVO)me.getValue()).getCount());
  13.                     else if(status.equals("Fixed"))
  14.                         System.out.println("WorkPackageName: "+name+"  Current Defect Status: "+status+"  Count: "+((WorkPackageSummaryVO)me.getValue()).getCount());
  15.                     else if(status.equals("Removed"))
  16.                         System.out.println("WorkPackageName: "+name+"  Current Defect Status: "+status+"  Count: "+((WorkPackageSummaryVO)me.getValue()).getCount());
  17.                 }
  18.             }
  19.         }
  20.  
Jul 12 '07 #1
16 1984
itsraghz
127 New Member
It may be because of the number of entries in Iterator "itr". Probably it might have only one entry.

Can you just check on the number of entries actually present?
Jul 12 '07 #2
madhoriya22
252 Contributor
It may be because of the number of entries in Iterator "itr". Probably it might have only one entry.

Can you just check on the number of entries actually present?

I check it. Iterator has more than one element. But at the change of i from 0 to 1 while loop is not iterating again......I think at the change of i while loop should start from the begining.....I dont know why its happenin..
Jul 12 '07 #3
praveen2gupta
201 New Member
chech value of the v.size(), either this is not working or returned value is 1.
or
while(itr.hasNext()) check the return type of the itr.hasNext(), while loop will work till condition is true. This condition must return the boolean value.
Jul 12 '07 #4
madhoriya22
252 Contributor
chech value of the v.size(), either this is not working or returned value is 1.
or
while(itr.hasNext()) check the return type of the itr.hasNext(), while loop will work till condition is true. This condition must return the boolean value.

I check itr.hasNext()...for i = 1,2,3 it is returning false......but at the change of i the while loop should start from the begining....any suggestion??
Jul 12 '07 #5
r035198x
13,262 MVP
I check itr.hasNext()...for i = 1,2,3 it is returning false......but at the change of i the while loop should start from the begining....any suggestion??
Is there any relationship between iter and v?
Jul 12 '07 #6
r035198x
13,262 MVP
Is there any relationship between iter and v?
i.e Why are you using both a for and a while to do the looping?
Jul 12 '07 #7
madhoriya22
252 Contributor
i.e Why are you using both a for and a while to do the looping?
Actually v is a vector it contains workpackageNames.......So I am comparing name in vector and name in iterator......to get the no of counts corresponding to status in that workpackage.....plz tell me if there is any other way to do it......
Jul 12 '07 #8
r035198x
13,262 MVP
Actually v is a vector it contains workpackageNames.......So I am comparing name in vector and name in iterator......to get the no of counts corresponding to status in that workpackage.....plz tell me if there is any other way to do it......
change your loops and put the for inside the while. Once the loop runs for the first i, iter.hasNext is no longer true for subsequent values of i.
Jul 12 '07 #9
praveen2gupta
201 New Member
I check itr.hasNext()...for i = 1,2,3 it is returning false......but at the change of i the while loop should start from the begining....any suggestion??

Hi
Your reply
itr.hasNext()...for i = 1,2,3 it is returning false......

while condition is returning a false value so it will not enter in the loop. It should return a true value.

itr.hasNext() is not working or the itr does not have any data. Check itr.
Jul 12 '07 #10
r035198x
13,262 MVP
Hi
Your reply
itr.hasNext()...for i = 1,2,3 it is returning false......

while condition is returning a false value so it will enter in the loop. It should return a true value.

itr.hasNext() is not working or the itr does not have any data. Check itr.
The iterator could have values but it runs through all of them with i = 0 because of the next being called.
Jul 12 '07 #11
madhoriya22
252 Contributor
The iterator could have values but it runs through all of them with i = 0 because of the next being called.

If I initialize iterator after for loop and before while loop then, will it help me?........

I have already put for loop in while loop........but by doing that output was not according to requirement.......
Jul 12 '07 #12
r035198x
13,262 MVP
If I initialize iterator after for loop and before while loop then, will it help me?........

I have already put for loop in while loop........but by doing that output was not according to requirement.......
Do you understand the logic of the next and hasNext methods?
See here
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < v.size(); i++) {
  2. //i = 0;
  3.        while (iter.hasNext()) {
  4.           ....iter.next();
  5.        }
  6.        //while is done => iter.hasNext() is false
  7.        //now when the for index changes to 1, the while loop is not entered
  8.        //any more because iter.hasNext() is false;
  9. }
Jul 12 '07 #13
madhoriya22
252 Contributor
Do you understand the logic of the next and hasNext methods?
See here
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < v.size(); i++) {
  2. //i = 0;
  3.        while (iter.hasNext()) {
  4.           ....iter.next();
  5.        }
  6.        //while is done => iter.hasNext() is false
  7.        //now when the for index changes to 1, the while loop is not entered
  8.        //any more because iter.hasNext() is false;
  9. }
My last reply was half........here v is Vector and iterator is for HashMap

Anyway....got your point......Now its done.....Thanks a lot.....
Jul 12 '07 #14
joloms38
1 New Member
hi. there..

how to use while loop in java using an exit?....
my problem is, i can not exit the program using the loop given to me...
please help me.....
Jul 13 '07 #15
madhoriya22
252 Contributor
hi. there..

how to use while loop in java using an exit?....
my problem is, i can not exit the program using the loop given to me...
please help me.....

inside while loop put a condition when u want to exit........in that condition give System.exit(0)..........
Jul 13 '07 #16
itsraghz
127 New Member
inside while loop put a condition when u want to exit........in that condition give System.exit(0)..........
Don't use System.exit(0) unnecessarily. Instead try having a boolean variable as a "flag" and have a conditional execution based upon its value. Make sure you set the flag variables value once you have reached the condition.

Use the features sparingly. Just because its present does not mean you can use everywhere.
Jul 13 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2471
by: Xiaozhu | last post by:
I trid to use icc -axW to optimize the code, but it becomes much slower... 8sec/37sec before and after using this option. my cpu is Intel(R) Pentium(R) 4 CPU 2.40GHz the test code is just one...
5
3301
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
8
2378
by: Jeff | last post by:
Hello everybody, I was doing one of the exercises in the K&R book, and I got something really strange. Here's the source code: /* * Exercise 2-2 from the K&R book, page 42 */ #include...
47
5220
by: fb | last post by:
Hi Everyone. Thanks for the help with the qudratic equation problem...I didn't think about actually doing the math...whoops. Anyway... I'm having some trouble getting the following program to...
1
1522
by: Promextheus Xex | last post by:
First php post... Hi there, I'm writing some code for my website to list people listening to my music. I have a loop for an array that dosen't seem to display all people. most of the time it...
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.