Checking for Empy TextFields
I, am trying to do a search on an arrayList of books. If the description textfield and the borrowerNumber textfield are empty, it should display all the books in the collections. Im having troubles comparing the borrowerText field with an null or "".
Any ideas in how can i do this?. This is the code that I've got so far.
int numberOfBookFound;int bookIndex;
publicvoid search()
{
int yourID = Integer.parseInt(borrowerTextField.getText());
String entry = descriptionTextField.getText();
index = 0; numberOfBookFound = 0; bookIndex = -1;
while(index < books.size()){
Book book = books.get(index);
if (book.getDescription().equals(null) && yourID == 0){
System.out.println(book.getDescription()); numberOfBookFound++; bookIndex = index;}
if (book.getDescription().contains(entry) && yourID == 0 && book.getBorrowerNumber() == 0){
System.out.println(book.getDescription()); numberOfBookFound++; bookIndex = index;}
if (borrowerTextField.getText().equals(null) && entry.equals(null)){
System.out.println(book.getDescription()) ; numberOfBookFound++; bookIndex = index;}
if (book.getBorrowerNumber() == yourID && entry.equals("") && book.getBorrowerNumber() != 0){
System.out.println(book.getDescription()) ; numberOfBookFound++; bookIndex = index;}
index++;
}
if (numberOfBookFound > 0){
found =true;
}
if (numberOfBookFound == 0){
JOptionPane.showMessageDialog(null,"NO BOOKS FOUND","Error",
JOptionPane.ERROR_MESSAGE);}
[2637 byte] By [
virkofa] at [2007-11-27 2:22:21]

borrowerTextField.getText().equals(null)
compares the text with null, which means there is not String. I don't think getText ever returns null, so this is probably unnecessary. To compare to "", meaning there was no text entered, use:
borrowerTextField.getText().equals("")
If you want to consider a text field with only whitespace as being empty, use:
borrowerTextField.getText().trim().equals("")
any idea in how can I display all the books from the collection if both textField are empty? I manage to do it with a try and catch, but when I try to do the search again, the program do not search again.
int numberOfBookFound; int bookIndex;
public void search()
{
try {
int yourID = Integer.parseInt(borrowerTextField.getText());
String entry = descriptionTextField.getText();
index = 0; numberOfBookFound = 0; bookIndex = -1;
while(index < books.size()){
Book book = books.get(index);
if (book.getDescription().equals(null) && yourID == 0){
System.out.println(book.getDescription()); numberOfBookFound++; bookIndex = index;}
if (book.getDescription().contains(entry) && yourID == 0 && book.getBorrowerNumber() == 0){
System.out.println(book.getDescription()); numberOfBookFound++; bookIndex = index;}
if (borrowerTextField.getText().trim().equals("") && entry.equals("")){
System.out.println(book.getDescription()) ; numberOfBookFound++; bookIndex = index;}
if (book.getBorrowerNumber() == yourID && entry.equals("") && book.getBorrowerNumber() != 0){
System.out.println(book.getDescription()) ; numberOfBookFound++; bookIndex = index; }
index++;
}
if (numberOfBookFound > 0){
found = true;
}
if (numberOfBookFound == 0){
JOptionPane.showMessageDialog(null, "NO BOOKS FOUND", "Error",
JOptionPane.ERROR_MESSAGE); }
}
catch(Exception e){
while(index < books.size()){
Book book = books.get(index);
System.out.println(book.getDescription()); index++; numberOfBookFound++;}}
}