Problem with "if - else" statement in for loop  
Author Message
jcastile





PostPosted: 2004-12-21 22:05:00 Top

java-programmer, Problem with "if - else" statement in for loop I am having problems with this section of code...

public void updateRank(){
int x = -1;
int y = -1;
String winnerMemNum = JOptionPane.showInputDialog
("Enter winner's membership number:");

int wMemNum = Integer.parseInt(winnerMemNum);

for(int i=0;i<list.size();i++){
Player temp = (Player)list.get(i);
if(temp.getMemberNumber() == wMemNum)
x = i;
else
System.out.println("Player not found");
break;
}

I want the condition in the if statement to be tested for every element
in the list before the code in the else block executes, but at the
moment it is only testing the first element. I know I need to put
brackets in, but when I did that, the whole thing stopped working! Any
ideas?

 
VisionSet





PostPosted: 2004-12-21 22:16:00 Top

java-programmer >> Problem with "if - else" statement in for loop
<email***@***.com> wrote in message
news:email***@***.com...
...
>
> I want the condition in the if statement to be tested for every element
> in the list before the code in the else block executes, but at the
> moment it is only testing the first element. I know I need to put
> brackets in, but when I did that, the whole thing stopped working! Any
> ideas?


> public void updateRank(){
> int x = -1;
> int y = -1;
> String winnerMemNum = JOptionPane.showInputDialog
> ("Enter winner's membership number:");
>
> int wMemNum = Integer.parseInt(winnerMemNum);

java.util.Iterator iterator = list.iterator();
boolean playerFound = false;
while(iterator.hasNext()) {
Player player = (Player) iterator.next();
if(player.getMemberNumber() == wMemNum) {
playerFound = true;
break;
}
}
if(! playerFound) {
System.out.println("Player not found");
}

--
Mike W