Question

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?)

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top