Search code examples
javaswingjtextfield

Check user entered string against String and Int


I'm trying to accept a user field in the form of a jTextArea (Search box). Then take this text and compare it against an ID OR Name and return if its inside of any.

  • Compare User Entry to ID
  • Compare User Entry to Name

Essentially check the user entry against a String and an Int.

I've got the following however am getting NumberFormatException.

String name = "Window";
int id = 12;
if (name.contains(searchText.getText().toLowerCase()) || id == Integer.valueOf(searchText.getText().replaceAll("[^0-9]", ""))) {
                    // TRUE STATEMENT
                }

So if a user enters "Win" it will return true. If they enter "test" it will return false. However if they enter "1","2" or "12" it will return true since the ID contains these.

I think I'm overthinking this one and could use some help. Thanks in advance


Solution

  •  if (name.toLowerCase().contains(searchText.getText()) 
                        || Integer.toString(id).contains(searchText.getText())) {
                    System.out.println("TRUE");
                 }