1

I have an array list named myArraylist that contains items of the class named TStas. Tstas has a string variable named st_name. I want to search the array list, looking for the TStas instance whose st_name is equal to the string look for and when found return the position (found places) of the TStas in the array list.

public static List<Integer> findplace_byname(String lookfor){

    List<Integer> foundplaces = new ArrayList<>(); //list to place posistions of found items

    for(int k=0; k<myArraylist.size(); k++) {
          TStas a=myArraylist.get(k);
          JOptionPane.showMessageDialog(null, "#"+a.st_name+"#"+lookfor+ "#"); //just to check if everything is read,

         if ((a.st_name).equals(lookfor)){

          foundplaces .add(k);
          }
     }
     return foundplaces;
    }

My problem is that the code fails to detect the equality when comparing to the a.st_name of the first item in myArraylist.

For example:

if I have in myarrailist an item with a.st_name=S9, an item with a.st_name=K9 and another with a.st_name=G4. When lookfor is K9 or G4 all is ok. When searching for the first item in the array having a.st_name=S9 the code fails to "see" the equality. I am using the showMessageDialog to check that the variable is realy read and it is so. Also I tried to delete or change the 1st item in the arraylist, but the same problem goes on: The 1rst item is not found.

What is happening here?

EDIT I used the trim() to remove any possible spaces but nothing changed. I then used .length() on the "trimed" string to get the length of each string to be compared and I found that for some reason the 1st element while being "S9" without any spaces has a length of 3!! Is it possible that some king of character is hidden? (I have no idea, a paragraph character or what?)

geo
  • 517
  • 1
  • 9
  • 28
  • 4
    I don't see a problem in what you've shown us, but you haven't shown us the full program. Are you sure that the code which fills your arraylist is correctly setting element 0? – keshlam Dec 31 '13 at 05:26
  • are you sure there are no leading/trailing space for the first element's string, it is exactly equal ? – jmj Dec 31 '13 at 05:28
  • This post may help you http://stackoverflow.com/questions/2167602/optimum-way-to-compare-strings-in-javascript – Phil Dec 31 '13 at 05:29
  • to follow up on @JigarJoshi comment, try using if ((a.st_name.trim()).equals(lookfor.trim())) – Josh Engelsma Dec 31 '13 at 05:29
  • also.. apart from the leading/trailing spaces, are you sure the case is also right??.. else you can use equalsIgnoreCase(). equals() is case sensitive.. – TheLostMind Dec 31 '13 at 05:31
  • The arraylist is populated using a streamreader from a text file, so it would be dificult to place the whole code here. But I can see that element 0 is set correctly using the showMessageDialog. I tried to use trim but same problem. Anyway the 2nd the 3rd etc elements are always found, but when I delete the 1st, the "new 1st" is not found. Also no issue with case sensitive (all is caps). – geo Dec 31 '13 at 05:48
  • Please check for following conditions 1. Cases when doing a comparison 2. Whitespaces 3. Whether 0th index element is `null` A question: How are you removing first element [Index 0] from list? – D3V Dec 31 '13 at 06:30
  • @ D3V. I remove it by deleting it from the text file used to populate the arraylist. – geo Dec 31 '13 at 06:35
  • Maybe be, first value being read from text file is not getting inserted into ArrayList. Because for me this code snippet looks good. Must be a problem with code that reads text and deserialize it to List. – D3V Dec 31 '13 at 06:37
  • Have a look at my edit. It seems that there is a "hidden" character in the problematic string. What could it be since it is not a space? – geo Dec 31 '13 at 06:45
  • It could the line separator character in UTF-8. – D3V Dec 31 '13 at 09:10
  • first of all try to log a.st_name and lookfor variables and see manually if they are same with no spaces(trimmed certainly). – Ashish Jan 01 '14 at 06:45

2 Answers2

0

There is no issue in your current code, check this code your self.

 List<Integer> foundplaces = new ArrayList<>(); 
 List<String>  myArraylist=new ArrayList<>();
 myArraylist.add("S9");
 myArraylist.add("K9");
 myArraylist.add("G4");
 for(int k=0; k<myArraylist.size(); k++) {
    String a=myArraylist.get(k);
    JOptionPane.showMessageDialog(null, "#" + a + "#" + "S9" + "#"); 
    if ((a).equals("S9")){
       foundplaces .add(k);
       System.out.println(k);
    }
}

You can see it is working fine. same as your current code.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

I found where the problem is.

As I mentioned is a comment I am using a txt file to populate myarraylist . Windows notepad ads automatically to the beginning of text files a BOM character. (http://en.wikipedia.org/wiki/Byte_Order_Mark.). This character is the problem because I may read "S9" (the first text in the txt file) but it actually is the \65279 character plus "S9". So using the following when reading the text file that is used to populate myarraylist the problem is solved.

    if((int)readingstring.charAt(0)==65279){
       readingstring=readingstring.substring(1); 
    }

Thanks for your help.

geo
  • 517
  • 1
  • 9
  • 28