while loop problem

Discussion in 'Java' started by roohbir, Mar 3, 2007.

  1. roohbir

    roohbir Guest

    Hello all,
    I have this code where I am required to use a while loop instead of a
    for loop to achieve the same result ( it asks the user to enter Yza or
    noname; if Yza is entered it looks for that in the array and displays
    that the name was found). I am having problems with the while loop .
    The initial for loop code is commented out. Can anyone please point
    out the error?
    Thanks a lot.
    ros

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    import javax.swing.JOptionPane;

    public class ForLoop {

    public static void main(String[] args) {
    // Declare and initialize String array variable called names.
    String names
    []={"Beah","Bianca","Lance","Belle","Nico","Yza","Gem","Ethan"};

    // This is the search string we are going to use to search the
    array.
    String searchName = JOptionPane.showInputDialog("Enter either
    \"Yza\" or \"noname\"!");

    // Declare and initialize boolean primitive type variable
    calld foundName.
    boolean foundName =false;

    int counter = 0;
    while ( counter < 7 ){
    if (names [counter].equals(searchName)){
    counter++;
    foundName = true;
    break;
    }
    }



    /*for (int i=0; i<names.length; i++){
    if (names [i ].equals(searchName)){
    foundName =true;
    break;
    }
    }*/

    // Display the result
    if (foundName)
    JOptionPane.showMessageDialog(null, searchName + " is
    found!");
    else
    JOptionPane.showMessageDialog (null, searchName + " is not
    found!");

    }

    }
    roohbir, Mar 3, 2007
    #1
    1. Advertising

  2. And thus spoke roohbir...

    > int counter = 0;


    Ok, you start at 0...

    > while ( counter < 7 ){


    as long as you are not > 6...

    > if (names [counter].equals(searchName)){
    > counter++;
    > foundName = true;
    > break;
    > }


    Ok, IF you find a name, you increase the counter... But what happens if
    you don't find one? In the next round, you will check the SAME name
    again - and again - and again...

    You must increase your counter, no matter if you found a name or not :)

    Flo
    Flo 'Irian' Schaetz, Mar 3, 2007
    #2
    1. Advertising

  3. roohbir

    Daniel Pitts Guest

    On Mar 3, 4:27 am, Flo 'Irian' Schaetz <> wrote:
    > And thus spoke roohbir...
    >
    > > int counter = 0;

    >
    > Ok, you start at 0...
    >
    > > while ( counter < 7 ){

    >
    > as long as you are not > 6...
    >
    > > if (names [counter].equals(searchName)){
    > > counter++;
    > > foundName = true;
    > > break;
    > > }

    >
    > Ok, IF you find a name, you increase the counter... But what happens if
    > you don't find one? In the next round, you will check the SAME name
    > again - and again - and again...
    >
    > You must increase your counter, no matter if you found a name or not :)
    >
    > Flo


    Also, your for loop checked "i < names.length", perhaps your while
    loop should use "count < names.length"
    Daniel Pitts, Mar 4, 2007
    #3
    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. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,225
    Tim Slattery
    Mar 30, 2005
  2. Daniel Pitts
    Replies:
    14
    Views:
    1,228
    Patricia Shanahan
    Dec 23, 2006
  3. Uday Bidkar
    Replies:
    4
    Views:
    484
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  4. Roedy Green
    Replies:
    3
    Views:
    424
    Mike Schilling
    Sep 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page