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. Advertisements

  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. Advertisements

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. Kelvin
    Replies:
    1
    Views:
    3,991
    Kelvin
    Sep 13, 2003
  2. Replies:
    25
    Views:
    959
    MonkeeSage
    Oct 1, 2006
  3. Replies:
    21
    Views:
    1,364
    Giannis Papadopoulos
    Aug 2, 2005
  4. kj
    Replies:
    15
    Views:
    794
    Lawrence D'Oliveiro
    May 23, 2009
  5. A
    Replies:
    8
    Views:
    1,051
    James Kanze
    Aug 28, 2010
  6. Raphael Gluck

    if.. else.. statement problem

    Raphael Gluck, Aug 3, 2004, in forum: ASP General
    Replies:
    5
    Views:
    213
    Bob Barrows [MVP]
    Aug 3, 2004
  7. Gergely Kontra
    Replies:
    16
    Views:
    413
    Devin Mullins
    Sep 20, 2005
  8. Isaac Won
    Replies:
    9
    Views:
    751
    Ulrich Eckhardt
    Mar 4, 2013
Loading...