Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > while loop problem

Reply
Thread Tools

while loop problem

 
 
roohbir
Guest
Posts: n/a
 
      03-03-2007
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","Ge m","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!");

}

}

 
Reply With Quote
 
 
 
 
Flo 'Irian' Schaetz
Guest
Posts: n/a
 
      03-03-2007
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
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      03-04-2007
On Mar 3, 4:27 am, Flo 'Irian' Schaetz <(E-Mail Removed)> 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"

 
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
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
Opinion poll: for loop vs while loop with Iterators. Daniel Pitts Java 14 12-23-2006 05:22 AM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM



Advertisments