user1419012
user1419012

Reputation: 21

comparing strings in an ArrayList. Case Sensative

This is my first post. I have spent hours trying to work out how I can compare my String i.e. word to all my ArrayList elements, checking if they are equal, disregarding case.

Here is my code.

private static void writeFile(Scanner scanWords, ArrayList<String> wordList,
  FileWriter fo, BufferedWriter out) throws IOException {
    String word = null;
    int count = 0;
    boolean contains=false;
    String temp = "";

    for(int i = 0; i<wordList.size();i++){

        while(scanWords.hasNext()){
            word= scanWords.next();

            if(wordList.get(i).equalsIgnoreCase(word)){
                contains = true;
            }else{
                System.out.println(word);
                out.write(word);
                //out.write(word);
            }
        }
    }
    count++;
    out.close();
}

Any help given would be amazing.

Upvotes: 0

Views: 1875

Answers (3)

amaidment
amaidment

Reputation: 7268

First, convert your List<String> to a HashSet of case insensitive strings - i.e. all lower case (using the String.toLowerCase() method). Then you can use the Collection.contains() method

e.g.

List<String> strings;
Set<String> lowerCaseStrings = new HashSet<String>(strings.size());
for (String str : strings){
  lowerCaseStrings.add(str.toLowerCase());
}
boolean match = lowerCaseString.contains(myStr.toLowerCase());

EDIT: Was suggesting using ArrayList<String>, but should be HashSet<String> - see comments below.

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691715

The problem comes from the fact that you're trying to do 3 different things, in the wrong order, in the same method:

  • write to a file
  • find a word in a list
  • read words from a scanner

Each of these operations should be a separate one. Regarding what you asked for:

how I can compare my String i.e. word to all my ArrayList elements, checking if they are equal, disregarding case

This is one part of your complete program that should be in its own method (the second bullet in the above list). You don't need a Scanner and a FileWriter and a BufferedWriter for this. All you need is the word and the list. And the method should return a boolean:

public boolean listContainsWordIgnoringCase(List<String> list, String word) {

And the implementation is very easy:

public boolean listContainsWordIgnoringCase(List<String> list, String word) {
    for (String element : list) {
        if (word.equalsIgnoreCase(element)) {
            // no need to continue looping. We've found the answer
            return true;
        }
    }
    // we've looped through all the elements without finding a match
    return false;
}

Now this method can be used in your program.

The trick is to separate the logic in simple blocks which only have one responsibility. And make a method for each of this block.

Upvotes: 1

Ravinder Reddy
Ravinder Reddy

Reputation: 24002

how i can compare my String i.e. word to all my ArrayList elements ...

It seems, you are using scanWords scanner object incorrectly.

Using your code, if first element of your wordsList does not match with any of scanWords elements, then none of the elements in wordsList will be processed for comparison. Why because, to compare with first word in the list, you have iterated till the end of scanWords which causes a next call to hasNext() always return a false.

Following is a fix to your requirement:

while ( scanWords.hasNext() ) {
  word= scanWords.next();
  //for( int i = 0; i < wordList.size(); i++ ) {
  for( String wordInList : wordList ) {
    if( wordInList.equalsIgnoreCase( word ) ) {
      contains = true;
    } else {
      System.out.println( word );
      out.write( word );
    }
  } // for list
} // while scanner

Solution 2:

I tried this method before, the problem is it loops through the same word 6 times. 6 being the size of my arraylist.

Here is a simple solution without explicit looping on list elements.

String listElements = wordList.toString().toLowerCase();  
while ( scanWords.hasNext() ) {  
  word = scanWords.next();  
  if ( listElements.contains( word.toLowerCase() ) ) {  
    contains = true;  
  } else {  
    System.out.println( word );  
    out.write( word );  
  }  
} // while scanner  

Sample Execution:

List<String> ls = new ArrayList<String>( 4 );
ls.add( "raVi" );
ls.add( "sreekaR" );
ls.add( "lakShMi" );
ls.add( "sumAna" );

String listElements = ls.toString();
System.out.println( "listElements: " + listElements );
System.out.println( "listElements.toLowerCase(): " + listElements.toLowerCase() );
scanner = new Scanner( "Ravi Sumana Others sREEKar LAKshmi" );
while( scanner.hasNext() ) {
  String w1 = scanner.next();
  System.out.println( "'" + w1 + "' is in list ? = " + listElements.toLowerCase().contains( w1.toLowerCase() ) );
}

And the result on execution is:

listElements: [raVi, sreekaR, lakShMi, sumAna]
listElements.toLowerCase(): [ravi, sreekar, lakshmi, sumana]
'Ravi' is in list ? = true
'Sumana' is in list ? = true
'Others' is in list ? = false
'sREEKar' is in list ? = true
'LAKshmi' is in list ? = true

Upvotes: 0

Related Questions