Some Java Guy
Some Java Guy

Reputation: 5118

user input ignore case

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.

Upvotes: 0

Views: 16990

Answers (5)

lukastymo
lukastymo

Reputation: 26819

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.

Upvotes: 0

user330315
user330315

Reputation:

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.

Upvotes: 2

josefx
josefx

Reputation: 15656

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

Upvotes: 0

corsiKa
corsiKa

Reputation: 82589

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

Upvotes: 0

MAK
MAK

Reputation: 26586

equalsIgnoreCase is a method of the String class.

Try

someString.equalsIgnoreCase(bufRead.readLine());

Upvotes: 0

Related Questions