Question

I am reading a user input. I was wondering how I would apply equalsIgnoreCase to the user input?

 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.
Was it helpful?

Solution

If you don't need a List you could use a Set initialized with a case-insensitive comparator:

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

Now when you call remove, the case of the argument no longer matters. So both of the following lines would work:

colors.remove("RED");

or

colors.remove("Red");

But this will only work if you don't need the ordering that the List interfaces gives you.

OTHER TIPS

equalsIgnoreCase is a method of the String class.

Try

someString.equalsIgnoreCase(bufRead.readLine());

IF you want to ignore the case, you can't do it when you retrieve.

Instead, you need to move it to all caps or all lowercase when you put it in the list.

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

Then, you can do later

aListColors.remove(rem.toUpperCase());

Since the ArrayList.remove method uses equals instead of equalsIgnoreCase you have to iterate through the list yourself.

Iterator<String> iter = aListColors.iterator();
while(iter.hasNext()){
     if(iter.next().equalsIgnoreCase(rem))
     {
        iter.remove();
        break;
     }
}

Method remove in collections is implemeneted to remove elements in equals() meaning so "Red".equals("red") is false and you can't find method which has equalsIgnnoreCase in List. This would have sense only for String so you can write your own class and add equals method - what is equals to you

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"));
    }
}

Or solution without override equals: write method which iterate through list and find your "red" in equalsIgnoreCase way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top