简体   繁体   English

根据 String 和 Int 检查用户输入的字符串

[英]Check user entered string against String and Int

I'm trying to accept a user field in the form of a jTextArea (Search box).我正在尝试接受 jTextArea (搜索框)形式的用户字段。 Then take this text and compare it against an ID OR Name and return if its inside of any.然后获取此文本并将其与 ID 或名称进行比较,如果它在任何内部,则返回。

  • Compare User Entry to ID将用户条目与 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.我有以下但是得到 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.因此,如果用户输入“Win”,它将返回 true。 If they enter "test" it will return false.如果他们输入“test”,它将返回 false。 However if they enter "1","2" or "12" it will return true since the ID contains these.但是,如果他们输入“1”、“2”或“12”,它将返回 true,因为 ID 包含这些。

I think I'm overthinking this one and could use some help.我想我想太多了,可以使用一些帮助。 Thanks in advance提前致谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM