DotSlashShell
DotSlashShell

Reputation: 35

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.

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

Upvotes: 0

Views: 55

Answers (1)

upog
upog

Reputation: 5531

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

Upvotes: 2

Related Questions