Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

while loop problem

Thread view: 
roohbir - 03 Mar 2007 10:36 GMT
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!");

   }

}
Flo 'Irian' Schaetz - 03 Mar 2007 12:27 GMT
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
Daniel Pitts - 04 Mar 2007 09:42 GMT
> And thus spoke roohbir...
>
[quoted text clipped - 19 lines]
>
> Flo

Also, your for loop checked "i < names.length", perhaps your while
loop should use "count < names.length"


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.