can some please help me with this?
My question is, how can i stop the for loop from executing the else statement every time it cannot find a match for the searchInput? even if the for loop found a match for searchInput the for loop still executes the else statement. how can i deal with this?
here's my original code:
Code :
// The For loop executes the Else statement until a match is found for the if statement, how can i deal with this?
public void searchBook()
{
String searchInput = JOptionPane.showInputDialog(null, "Enter Search Query: ", "Book Search", JOptionPane.INFORMATION_MESSAGE);
int x;
try
{
for(x=0 ; x<bookName.length ; x++)
{
if(searchInput.equalsIgnoreCase(bookName[x]))
{
JOptionPane.showMessageDialog(null, "Book Found !" + "\nTitle: " + bookName[x] + "\nAuthor: " + bookAuthor[x] +
"\nPublisher: " + bookPublisher[x] + "\nBook ISBN: " + bookID[x] , "Book Search", JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "BOOK NOT FOUND !" , "Book Search", JOptionPane.ERROR_MESSAGE);
}
}
}
catch(Exception e)
{
}
}
thanks in advance... :))
Re: can some please help me with this?
Maybe someone could.. if you had a question ;)
Re: can some please help me with this?
my question is up there. in the code section at the very top.
Re: can some please help me with this?
Okay, how about you post a question outside of the code block, and post one with better detail.
If that is your original code, then "how do I deal with it" doesn't make much sense seeing as you're the one who put the else in there.
Re: can some please help me with this?
i edited my post. can you help me now? :))
Re: can some please help me with this?
2 Simple ways to do it, you can either:
Remove the else all together, and instead use a boolean variable which is set to true if a match is found, but outside the loop you test if(!matchfound) and put the fail code in there; OR
Replace else with else if, and test if the current X variable is going to be the last time the loop executes and then print.
Re: can some please help me with this?
well that was easy. THANKS ! :))