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]
# 1

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("")

hunter9000a at 2007-7-12 2:26:23 > top of Java-index,Java Essentials,Java Programming...
# 2

The problem is that the borrower is an integer. Is fine comparing the string description, but I have problems with the int.

how can i convert the int to a string?

When I try to use that code this error comes up

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

any ideas?

virkofa at 2007-7-12 2:26:23 > top of Java-index,Java Essentials,Java Programming...
# 3
It sounds like you are trying to convert a String into an int, not an int into a String.And when the String is empty (""), parsing will fail. I thought you were testing for that, first?
DrLaszloJamfa at 2007-7-12 2:26:23 > top of Java-index,Java Essentials,Java Programming...
# 4
Compare the String to "" before parsing the int from it. If it's not equal to "", then parse it, if it is, then do whatever you do when the user doesn't enter a borrower number.
hunter9000a at 2007-7-12 2:26:24 > top of Java-index,Java Essentials,Java Programming...
# 5

> any ideas?

String input = borrowerTextField.getText().trim();

if (input.isEmpty()) {// or "".equals(input), etc...

... empty input case ...

} else {

...non-empty input case

}

Does that make sense?

DrLaszloJamfa at 2007-7-12 2:26:24 > top of Java-index,Java Essentials,Java Programming...
# 6

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++;}}

}

virkofa at 2007-7-12 2:26:24 > top of Java-index,Java Essentials,Java Programming...
# 7
Still giving me the same error. :(
virkofa at 2007-7-12 2:26:24 > top of Java-index,Java Essentials,Java Programming...
# 8
int yourID = Integer.parseInt(borrowerTextField.getText());You're still parsing the input without checking for blank input.
hunter9000a at 2007-7-12 2:26:24 > top of Java-index,Java Essentials,Java Programming...
# 9
how can I check for blank input if is an int?
virkofa at 2007-7-12 2:26:24 > top of Java-index,Java Essentials,Java Programming...
# 10

> how can I check for blank input if is an int?

Reply 5 shows you how to do this. You get a String from getText. A String can be blank, or it can have a number in it (or have many other characters, but we'll ignore that for now). If it's blank, you don't want to try to get a number out of it. If it's not blank, then you can try to get a number out of it.

hunter9000a at 2007-7-12 2:26:24 > top of Java-index,Java Essentials,Java Programming...