2

How can I check in an arraylist for the same String while ignoring cases? Sorry in advance, I'm new at StackOverflow.

NEW EDIT: this is what I did so far with NetBeans at school. Thank You All! So what my teacher wants is the following: /* Ask for names and the entered names have to be sorted without doublets. means if the same name is entered twice, the second one will not appear and added to the arrayList. The first Letter has to be upper cased the rest lower. Additional: even with different input spellings. Example: Mike -> miKE -> Megan -> Lucy -> STOP sout: Lucy, Megan, Mike. Addition_2: sout only multiple entered names. */

ArrayList<String> listednames = new ArrayList<>();

while (true) {
    String entry = JOptionPane.showInputDialog("name:");
    if (entry == null) {
        break;
    }
    entry = entry.trim();
        String firstLetter = entry.substring(0,1).toUpperCase();
        String end = entry.substring(1).toLowerCase();
        String whole = firstLetter + end;

    if (entry.equalsIgnoreCase("stop")) {
        break;
    }
    if (entry.isEmpty()) {
        continue;
    }
    if (listednames.contains(entry)) { // .equalsIgnoreCase wont work with lists
        continue;
    }

    listednames.add(entry);
    Collections.sort(listednames);
    String namen = listednames.toString();
    namen = namen.substring(1, namen.length()-1);
    System.out.println(namen);

}
4
  • What do you mean by ignoring cases? If you mean to compare 2 strings regardless of whether each character is upper or lower case, then you simply just set both strings to all lower (or upper) case characters and then compare them. Commented Jul 19, 2017 at 13:34
  • What are you trying to accomplish? Do you want to store only elements which are unique (even if we ignore case)? Is order of elements important (if we add "ab" and "cd" should it be ordered like that or order "cd", "ab" is also fine)? Commented Jul 19, 2017 at 13:41
  • sorry, my fault I didn't explain what this all is for. I'm a student and we learn to code on NetBeans with Java. I'll edit the question and explain what is wanted. Thank You All For so many and quick answers. I appreciate it. Commented Jul 19, 2017 at 21:04
  • @Cesc if an answers satisfies you, you can accept it, if not you may comment ont to ask for improve or edit your post Commented Jul 20, 2017 at 13:09

4 Answers 4

4

You have severals options :

1. If it doesn't matter at the end, you can always add the element in lowerCaser

listednames.add(entry.toLowercase());

And so do entry = entry.trim().toLowercase() like this your contains check will work

2. Check manually with a method :

for (String name : listednames) {
    if (name.equalIgnoreCase(entry)) {
        continue;
    }
}

Or using java 8 features :

if( listednames.stream().anyMatch(entry::equalsIgnoreCase) ){
      continue;
}
Sign up to request clarification or add additional context in comments.

2 Comments

use anyMatch instead of filter
You can also use entry::equalsIgnoreCase with anyMatch
2

You'll have to manually loop through the ArrayList and compare the entries one-by-one.

for (String s : listednames) {
    if (s.equalIgnoreCase(entry)) {
        continue;
    }
}

Comments

0

If you want to do it in a linear time is very simple. You should use toUpperCase() (or toLowerCase()) in the check and in the add method, you can try with this modified code:

ArrayList listednames = new ArrayList<>();

while (true) {
    String entry = JOptionPane.showInputDialog("name:");
    if (entry == null) {
        break;
    }
    entry = entry.trim();

    if (entry.equalsIgnoreCase("stop")) {
        break;
    }
    if (entry.isEmpty()) {
        continue;
    }
    if (listednames.contains(entry.toUpperCase())) {
        continue;
    }

    listednames.add(entry.toUpperCase());
    Collections.sort(listednames);
    String namen = listednames.toString();
    namen = namen.substring(1, namen.length()-1);
    System.out.println(namen);
}

Comments

0

If you like it short:

if (listednames.stream().anyMatch(entry::equalsIgnoreCase)) {

Although the for loop is usually preferrable to using a stream for readability, performance and debugging, just thought I'd throw it in here.

And because I do prefer fewer lines (and you may not be aware of this):

String entry = JOptionPane.showInputDialog("name:");
entry = Optional.ofNullable(entry).orElse("STOP").trim();
if ("STOP".equalsIgnoreCase(entry)) {
    break;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.