-1

Sorry if this is an obvious problem.

I'm trying to read strings from users and store them in an array. but I want to Ignore the if case (i.e., consider “The” and “the” as the same word)

Thanks a lot for any help!

Scanner fileScanner = new Scanner(file);
        fileScanner.useDelimiter("[^A-Za-z0-9]");
        ArrayList<String> words = new ArrayList<String>();
        while (fileScanner.hasNext()) {
            String nextWord = fileScanner.next();
            if (!words.contains(nextWord) ) {
                words.add(nextWord);
            }
        }
Yasser BK
  • 23
  • 4
  • 3
    Store them in the list as lowercase? – Scary Wombat Dec 07 '21 at 02:28
  • 2
    Welcome to Stack Overflow. What do you mean by "ignore"? When you use your code, what exactly happens to the `words`, and exactly how is that different from what is supposed to happen? And if you *only* want to "store them in an array", then why do you need any other logic like `!words.contains(nextWord)`? Please try to explain the task clearly. – Karl Knechtel Dec 07 '21 at 02:36
  • When the user enters "The" or "the" I want them to be defined as one word, not two @KarlKnechtel – Yasser BK Dec 07 '21 at 03:04
  • Search Stack Overflow before posting. – Basil Bourque Dec 07 '21 at 05:19

2 Answers2

1
    Scanner fileScanner = new Scanner(file);
    fileScanner.useDelimiter("[^A-Za-z0-9]");
    Map<String,String> words = new HashMap<>();
    while (fileScanner.hasNext()) {
        String nextWord = fileScanner.next();
        words.put(nextWord.toLowerCase(), nextWord);
    }

Key points:

  1. Store lower cased words

  2. Convert input to lower case before looking it up

  3. Use a HashMap rather than an ArrayList - much better performance for 'contains'. The key is the lowercased word, the value is what the user actually typed.

  4. Given we're using a Map, we don't even need to check 'contains' since the Set automatically ensures only one instance of equal keys (it retains the most recently added instance, but that's ok here)

user16632363
  • 1,050
  • 3
  • 6
  • 1
    I have to display what the user wrote again So I can't convert letters to lowercase – Yasser BK Dec 07 '21 at 02:54
  • So if the user enters "Foo" and "foo", and you're only storing one, which is the one you display as "what the user wrote". – user16632363 Dec 07 '21 at 18:36
  • But in any case, it's easily rectified by using a Map, where the lowercase form is the key, and the value stores the word as the user actually typed it. – user16632363 Dec 07 '21 at 18:40
0

You can use the toLowerCase function from String

Scanner fileScanner = new Scanner(file);

fileScanner.useDelimiter("[^A-Za-z0-9]");

Set<String> wordsCheck = new HashSet<>();
List<String> words = new ArrayList<>();
        
while (fileScanner.hasNext())
{
      
    String nextWord = fileScanner.next();
         
    if (!wordsCheck.contains(nextWord.toLowerCase()))
    {
         
       wordsCheck.add(nextWord.toLowerCase());
       words.add(nextWord);
         
    }
         
}
   
davidalayachew
  • 1,279
  • 1
  • 11
  • 22