R
roohbir
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!");
}
}
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!");
}
}