Hi Jason

There is a good start in your code:

   int i = 0;
   while ( i < names.length ) { ...


In the "while" loop, the first thing to do is, I presume, to check if the entered searchName fits the current names[i]. You must do it before incrementing i, as the first element in the array is names[0]. If there is a match (and only in this case) you have to set "fundName" to "true" and to exit.

To compare two String objects you can use the method "equals" or "equalsIgnoreCase". (You cannot use the "==" operator.)

This gives something like:

   int i = 0;
   while ( i < names.length ){
       if ( names[i].equals( searchName )){
           findName = true;
           break;
       }
       i++;
   }

There are several nice variants for this code (for e.g. lopping based on the value of "findName" and break on the value of i, and so on), I leave you the pleasure to experiment them.

Hope it helps
mihai


jason waite a écrit :
I am having difficulty converting this project from a "for" loop to a "while" loop. Everything that I input is "found". I am not sure where the problem is. The homework specifies to use a while loop. Does that mean that I could also use a do-while or is that not allowed/necessary? Here is what I have. Thanks in advance!




import javax.swing.JOptionPane;

public class MyOwnWhileProject {

    /** Creates a new instance of ForLoop */
    public MyOwnWhileProject() {
    }

    /**
     * @param args the command line arguments
     */
    @SuppressWarnings("empty-statement")
    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 i =0;
        while (i<names.length){
                i++;
                foundName =true;
                break;
}

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

    }

}
--
To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en To unsubscribe from this group, send email to javaprogrammingwithpassion+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.

--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

To unsubscribe from this group, send email to 
javaprogrammingwithpassion+unsubscribegooglegroups.com or reply to this email with the 
words "REMOVE ME" as the subject.

Reply via email to