Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problem with "if - else" statement in for loop

Reply
Thread Tools

Problem with "if - else" statement in for loop

 
 
jcastile@blueyonder.co.uk
Guest
Posts: n/a
 
      12-21-2004
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?

 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      12-21-2004

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
problem with loop statement bxbxb3 VHDL 8 03-18-2005 01:24 PM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments