Hi Mesfn
There is a problem in the organization of the while loop.
First of all, it is not a good practice to relay on the default value of
a static variable (i). What about if a couple of moth later you have to
modify your program and run the code in the "main" method twice. Does it
means that the "while ( i < ...) ..." will start with the latest value
of "i"? I would rather use a "for" loop (as I can keep all that concerns
the counter in a place), but a while loop would be as good as long the
counter is initialized just before.
So:
...
*boolean foundName = false; // just before the loop - Ok
int i = 0; // just before the loop too*
while ( i < names.length ){ ... }
...
Then: your array "names" has 8 elements (names.length is 8), counting
from 0 to 7 (names[0] is "Beah", names[1] is "Bianca", ..., names[7] is
"Ethan").
In your loop "while ( i < names.length ){ i++; ... }", the first thing
is you increment the counter. That means that for the first iteration,
your "i" is "0" (less than 8), then you increment it to 1 immediately,
then you check "names[1]". You never check "names[0]". Also, after
checking "names[7]", the while condition is still true (7 < 8), so one
more iteration is performed, "i" is increased to "8" and "names[8]" is
checked. Of course, this element doesn't exist and generates "Array
index out of bounds" error (as the index value is bound between 0 and 7
and a value of 8 is currently used).
So, the while loop should look like:
...
boolean foundName = false; // just before the loop - Ok
int i = 0; // just before the loop too
while ( i < names.length ){
* ... // check names[i] and break of found
i++; // increment the counter only after the check*
}
...
In the way your loop is written now, it exits anyway after the first
check (I think it is just one of the versions of your work, modified
like this in order to avoid the error message). Of course, you will want
to loop until you find the element or up to the end of the array, which
one comes first. So, your loop might look like:
...
boolean foundName = false; // just before the loop - Ok
int i = 0; // just before the loop too
while ( i < names.length ){
* // Written like this, foundName will be true if equal
// or false if different
foundName = names[i].equals( searchName );
if ( foundName ) break; // exit if found
* i++; // increment the counter only after the check
}
if ( foundName ) // This is outside the loop
JOptionPane.showMessageDialog(null, searchName + " found Ok");
else
JOptionPane.showMessageDialog(null, searchName + " not found");
...
Written like this, your don't really care about the value of the counter
"i" outside of the loop (you could care in order to displays searchName
+ " found as element" + i). If you don't care, you can simply include
the "foundName" flag in the while looping criteria:
...
boolean foundName = false; // just before the loop - Ok
int i = 0; // just before the loop too
while ( i < names.length *&& ! foundName *){ // loop while not found
// Written like this, foundName will be true if equal
// or false if different
foundName = names[i].equals( searchName );
i++; // increment the counter only after the check
}
if ( foundName )
JOptionPane.showMessageDialog(null, searchName + " found Ok");
else
JOptionPane.showMessageDialog(null, searchName + " not found");
...
Hope it helps
Mihai
mesfin a écrit :
I have a little problem in this home work which displaying the Correct
search result ,However it does not display the wrong search result on
the dialog...I got the following Error When i insert a wrong word in
to the input dialog.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at OwnWhileProject.main(OwnWhileProject.java:40)
Java Result: 1
Any one can fox the problem,
I appreciate
******************************************//
***********************************************
import javax.swing.JOptionPane;
/**
*
* @author mesfin
*/
public class OwnWhileProject {
private static int i;
public OwnWhileProject(){
}
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;
while ( i < names.length ) {
i++;
if (names [i ].equals(searchName)){
foundName =true;
if (foundName)
JOptionPane.showMessageDialog(null, searchName + " is
found!");
else
JOptionPane.showMessageDialog(null, searchName + " is
not found!");
break;
}
}
}
}
--
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