Java 用户输入忽略案例

Java 用户输入忽略案例,java,input,user-input,stdstring,ignore-case,Java,Input,User Input,Stdstring,Ignore Case,我正在读取用户输入。我想知道如何将equalsIgnoreCase应用于用户输入 ArrayList<String> aListColors = new ArrayList<String>(); aListColors.add("Red"); aListColors.add("Green"); aListColors.add("Blue"); InputStreamReader istream = new InputStreamReader(

我正在读取用户输入。我想知道如何将
equalsIgnoreCase
应用于用户输入

 ArrayList<String> aListColors = new ArrayList<String>();
    aListColors.add("Red");
    aListColors.add("Green");
    aListColors.add("Blue");

 InputStreamReader istream = new InputStreamReader(System.in) ;
 BufferedReader bufRead = new BufferedReader(istream) ;
 String rem = bufRead.readLine();  // the user can enter 'red' instead of 'Red'
 aListColors.remove(rem);  //equalsIgnoreCase or other procedure to match and remove.
ArrayList aListColors=new ArrayList();
添加(“红色”);
添加(“绿色”);
添加(“蓝色”);
InputStreamReader istream=新的InputStreamReader(System.in);
BufferedReader bufRead=新的BufferedReader(istream);
字符串rem=bufRead.readLine();//用户可以输入“红色”而不是“红色”
移除(rem)//EqualSignalCase或其他要匹配和删除的程序。

equalsIgnoreCase是String类的一个方法

试一试


如果要忽略该案例,则在检索时无法执行该操作

相反,当您将其放入列表时,需要将其移动到所有大写或小写

ArrayList<String> aListColors = new ArrayList<String>();
aListColors.add("Red".toUpperCase());
aListColors.add("Green".toUpperCase());
aListColors.add("Blue".toUpperCase());

由于ArrayList.remove方法使用equals而不是equalsIgnoreCase,因此您必须自己遍历列表

Iterator<String> iter = aListColors.iterator();
while(iter.hasNext()){
     if(iter.next().equalsIgnoreCase(rem))
     {
        iter.remove();
        break;
     }
}
Iterator iter=aListColors.Iterator(); while(iter.hasNext()){ if(iter.next().equalsIgnoreCase(rem)) { iter.remove(); 打破 } }
如果您不需要
列表
,您可以使用
集合
使用不区分大小写的比较器初始化:

Set<String> colors = 
      new TreeSet<String>(new Comparator<String>()
          { 
            public int compare(String value1, String value2)
            {
              // this throw an exception if value1 is null!
              return value1.compareToIgnoreCase(value2);
            }
          });

colors.add("Red");
colors.add("Green");
colors.add("Blue");


但这仅在您不需要<代码>列表<代码>接口提供的顺序时才起作用。

方法在集合中删除的是equals()中的元素,意思是“Red”。equals(“Red”)为false,并且您无法在列表中找到具有equalSignaneCase的方法。这只对字符串有意义,所以您可以编写自己的类并添加equals方法-什么是等于您的

class Person {
    String name;
    // getter, constructor
    @Override
    public boolean equals(Object obj) {
        return (obj instanceof Person && ((Person)obj).getName().equalsIgnoreCase(name));
    }
}

public class MyHelloWorld {
    public static void main(String[] args) {
        List<Person> list = new ArrayList<Person>();
        list.add(new Person("Red"));
        list.remove(new Person("red"));
    }
}
班级人员{
字符串名;
//getter,构造函数
@凌驾
公共布尔等于(对象obj){
返回(Person&((Person)obj.getName().equalsIgnoreCase(name))的obj实例;
}
}
公共类MyHelloWorld{
公共静态void main(字符串[]args){
列表=新的ArrayList();
列表。添加(新人员(“红色”);
名单。删除(新人员(“红色”);
}
}
或者不覆盖等于的解决方案:编写方法,在列表中迭代并以equalsIgnoreCase方式找到“红色”。

equalsIgnoreCase到什么?(另外,添加了java标记)
Set<String> colors = 
      new TreeSet<String>(new Comparator<String>()
          { 
            public int compare(String value1, String value2)
            {
              // this throw an exception if value1 is null!
              return value1.compareToIgnoreCase(value2);
            }
          });

colors.add("Red");
colors.add("Green");
colors.add("Blue");
colors.remove("RED");
colors.remove("Red");
class Person {
    String name;
    // getter, constructor
    @Override
    public boolean equals(Object obj) {
        return (obj instanceof Person && ((Person)obj).getName().equalsIgnoreCase(name));
    }
}

public class MyHelloWorld {
    public static void main(String[] args) {
        List<Person> list = new ArrayList<Person>();
        list.add(new Person("Red"));
        list.remove(new Person("red"));
    }
}