Problem with "if - else" statement in for loop

J

jcastile

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?
 
V

VisionSet

....
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");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top