Problem with "if - else" statement in for loop

Discussion in 'Java' started by jcastile@blueyonder.co.uk, Dec 21, 2004.

  1. Guest

    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?
    , Dec 21, 2004
    #1
    1. Advertising

  2. VisionSet Guest

    <> wrote in message
    news:...
    ....
    >
    > 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
    VisionSet, Dec 21, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    25
    Views:
    648
    MonkeeSage
    Oct 1, 2006
  2. Replies:
    21
    Views:
    1,032
    Giannis Papadopoulos
    Aug 2, 2005
  3. kj
    Replies:
    15
    Views:
    536
    Lawrence D'Oliveiro
    May 23, 2009
  4. A
    Replies:
    8
    Views:
    782
    James Kanze
    Aug 28, 2010
  5. Isaac Won
    Replies:
    9
    Views:
    350
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page